如何为网站创建rss feed?

时间:2010-05-15 06:04:10

标签: asp.net

我开发了一个webapplication,现在我想为我的网站创建Rss提要。 在我的应用程序中,我有一个模块调用电影新闻,其中包含电影明星的最新消息。现在我想为该模块创建rss feed。新闻包含标题和说明。如何为我的应用程序创建rss提要?

3 个答案:

答案 0 :(得分:4)

这是我用于我的Feed的代码,它只是一个.ashx(通用处理程序),我指向我的网站。

请记住将这一小段html添加到您的网站:

<link rel="alternate" type="application/rss+xml" href="/rss.ashx" title="Rss feed for yourdomain.com" /> 

这是完整的处理程序代码:

public class rssHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        var Response = context.Response;

        // Prepare response
        Response.Buffer = false;
        Response.Clear();
        Response.ContentType = "application/rss+xml";
        Response.Cache.SetExpires(DateTime.Now.AddMinutes(10));
        Response.Cache.SetCacheability(HttpCacheability.Public);

        // Create an XmlWriter to write the feed into it
        using (XmlWriter writer = XmlWriter.Create(Response.OutputStream))
        {
            // Set the feed properties
            SyndicationFeed feed = new SyndicationFeed
                ("yourdomain.com",
                "Some description of your feed",
                new Uri("http://www.yourdomain.com"));

            // Add authors
            feed.Authors.Add(new SyndicationPerson
                ("yourmail@yourdomain.com",
                "your name",
                "http://www.yourdomain.com"));

            // Add categories
            NewsType[] categories = (NewsType[])Enum.GetValues(typeof(NewsType)); // NewsType is a enum I use, which is custom created

            foreach (var category in categories)
            {
                feed.Categories.Add(new SyndicationCategory(category.GetDescription()));
            }

            // Set copyright
            feed.Copyright = new TextSyndicationContent
                ("© Copyright 2009 your name");

            // Set generator
            feed.Generator = "yourdomain.com";

            // Set language
            feed.Language = "da-DK";

            // Add post items
            List<SyndicationItem> items = new List<SyndicationItem>();
            var newsList = News.GetLatest(20);
            foreach (var news in newsList)
            {
                string url = GetShowUrl(news);
                SyndicationItem item = new SyndicationItem();
                item.Id = news.ID.ToString();
                item.Title = TextSyndicationContent.CreatePlaintextContent(news.Name);
                item.Content = SyndicationContent.CreateXhtmlContent(news.Content);
                item.PublishDate = news.DateCreated.Value;
                item.Categories.Add(new SyndicationCategory(news.NewsType.Value.GetDescription()));
                item.Links.Add(new SyndicationLink(new Uri(url), "alternate", news.Name, "text/html", 1000));
                items.Add(item);
            }
            feed.Items = items;

            // Write the feed to output
            Rss20FeedFormatter formatter = new Rss20FeedFormatter(feed);
            formatter.WriteTo(writer);

            writer.Flush();
        }
        Response.End();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    private string GetShowUrl(News news)
    {
        // Returns proper absolute URL to the item
    }
}

答案 1 :(得分:2)

答案 2 :(得分:0)

首先,你应该学习一个典型的RSS提要的格式,这是一个例子:

<?xml version="1.0"?>
<rss version="2.0">
  <channel>
    <title>Example Channel</title>
    <link>http://example.com/</link>
    <description>My example channel</description>
    <item>
       <title>News for September the Second</title>
       <link>http://example.com/2002/09/01</link>
       <description>other things happened today</description>
    </item>
    <item>
       <title>News for September the First</title>
       <link>http://example.com/2002/09/02</link>
    </item>
  </channel>
</rss>

接下来,创建一个脚本,在数据库中查询要包含的所有页面,然后在循环中输出它们:

//begin loop

<item>
   <title>[title]</title>
   <link>[link]</link>
   <description>[description]</description>
</item>

//end loop

完成后,将脚本中的输出保存为feed.rss并将其放在网络服务器的根目录下。