【问题标题】:how to create rss feed for a website?如何为网站创建 RSS 提要?
【发布时间】:2010-05-15 06:04:10
【问题描述】:

我开发了一个网络应用程序,现在我想为我的网站创建 Rss 提要。 在我的应用程序中,我有一个名为 Film news 的模块,其中包含电影明星的最新消息。现在我想为该模块创建 RSS 提要。新闻包含标题和描述。如何为我的应用程序创建 RSS 提要?

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    这是我用于提要的代码,它只是一个 .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
        }
    }
    

    【讨论】:

      【解决方案2】:
      【解决方案3】:

      首先你应该了解一个典型的 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 并将其放在网络服务器的根目录中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-13
        相关资源
        最近更新 更多