【问题标题】:How to add RSS feature to my web site?如何将 RSS 功能添加到我的网站?
【发布时间】:2010-09-03 10:56:26
【问题描述】:

我正在使用 VS2008 + C# + .Net 3.5 + IIS 7.0 + ASP.Net 来开发一个简单的 Web 应用程序。我想在我的网站的一些页面上添加 RSS 功能,以便人们可以使用他们流行的 RSS 阅读器来接收内容更新的通知。

在我的开发环境中有什么简单的方法可以做到这一点吗?我只需要非常基本的 RSS 功能。

【问题讨论】:

    标签: c# asp.net .net visual-studio-2008 rss


    【解决方案1】:

    我建议您使用 .NET 3.5 附带的新 Syndication API。这是来自MSDN How to: Create a Basic RSS Feed article 的示例:

    public class BlogService : IBlog
    {
        public Rss20FeedFormatter GetBlog()
        {
            SyndicationFeed feed = new SyndicationFeed("My Blog Feed", "This is a test feed", new Uri("http://SomeURI"));
            feed.Authors.Add(new SyndicationPerson("someone@microsoft.com"));
            feed.Categories.Add(new SyndicationCategory("How To Sample Code"));
            feed.Description = new TextSyndicationContent("This is a how to sample that demonstrates how to expose a feed using RSS with WCF");
    
            SyndicationItem item1 = new SyndicationItem(
                "Item One",
                "This is the content for item one",
                new Uri("http://localhost/Content/One"),
                "ItemOneID",
                DateTime.Now);
    
            SyndicationItem item2 = new SyndicationItem(
                "Item Two",
                "This is the content for item two",
                new Uri("http://localhost/Content/Two"),
                "ItemTwoID",
                DateTime.Now);
    
            SyndicationItem item3 = new SyndicationItem(
                "Item Three",
                "This is the content for item three",
                new Uri("http://localhost/Content/three"),
                "ItemThreeID",
                DateTime.Now);
    
            List<SyndicationItem> items = new List<SyndicationItem>();
    
            items.Add(item1);
            items.Add(item2);
            items.Add(item3);
    
            feed.Items = items;
    
            return new Rss20FeedFormatter(feed);
        }
    }
    

    【讨论】:

    • 此解决方案使用 WCF 为 RSS 创建单独的 Url。但我想将 RSS 提要集成到我自己的网站中。例如,在当前的stackoverflow Url中,我可以按RSS按钮直接消费RSS。
    • 例如,在本页的源代码中,您会发现以下行 &lt;link rel="alternate" type="application/atom+xml" title="Feed for question 'How to add RSS feature to my web site?'" href="/feeds/question/3635047"&gt; 此行启用浏览器中的 RSS 按钮,在您的情况下,您必须像这样输入提要的 URI 和标题: &lt;link rel="alternate" type="application/atom+xml" title="my feed" href="http://example.com/path/to/atom"&gt; 如果你使用原子 &lt;link rel="alternate" type="application/rss+xml" title="my feed" href="http://example.com/path/to/rss"&gt; 用于 rss
    【解决方案2】:

    有一个名为 RSS.Net 的开源 .net 类库。见http://www.rssdotnet.com/

    【讨论】:

    • 请看rssdotnet.com/documents/code_examples.html。在那里,您可以了解如何轻松为您的网站生成 rss 提要。
    • 创建一个网页,比如 rss.aspx。并在您的代码中(如示例中所述)将内容类型设置为“txt/xml”并将提要写入输出流。您可以在我之前评论中提供的链接中找到页面底部的相关代码。
    • rss.aspx 将不包含任何 html 代码,它将仅包含页面指令 ()... 在代码隐藏文件中,您将编写必要的 rss 生成器代码。这样你就可以得到这样的结果:julianhoffman.wordpress.com/feed
    • 这是内容页面:stackoverflow.com/questions/3635047/… 这是该页面的 RSS 提要:stackoverflow.com/feeds/question/3635047 所以,您需要两个单独的页面。一个用于内容,另一个用于 RSS 内容。在内容页面上,您可以放置​​一个指向该 rss 页面的链接,浏览器会自动识别这是 rss 内容并显示相关按钮,例如订阅等...
    • 查看页面底部。你会在那里看到问题提要链接:)。
    猜你喜欢
    • 2016-04-17
    • 2011-07-31
    • 2009-11-01
    • 1970-01-01
    • 2023-03-31
    • 2016-03-02
    • 2020-03-19
    • 1970-01-01
    • 2013-10-28
    相关资源
    最近更新 更多