您建议如何在 ASP.NET MVC 中处理 RSS 源?使用第三方库?在 BCL 中使用 RSS 内容?只是制作一个呈现 XML 的 RSS 视图?或者完全不同的东西?

有帮助吗?

解决方案

这是我的推荐:

  1. 创建一个称为RSSRESULT的类,该类从抽象的基类Action Result继承。
  2. 重写 ExecuteResult 方法。
  3. ExecuteResult 具有调用者传递给它的 ControllerContext,通过它您可以获取数据和内容类型。
  4. 将内容类型更改为 rss 后,您将需要将数据序列化为 RSS(使用您自己的代码或其他库)并写入响应。

  5. 在要返回 rss 的控制器上创建一个操作,并将返回类型设置为 RssResult。根据您想要返回的内容从模型中获取数据。

  6. 然后,对此操作的任何请求都将收到您选择的任何数据的 rss。

这可能是在 ASP.NET MVC 中返回 rss 对请求的响应的最快且可重用的方法。

其他提示

.NET 框架公开了处理聚合的类:联合供稿等因此,与其自己进行渲染或使用其他建议的 RSS 库,为什么不让框架来处理呢?

基本上,您只需要以下自定义 ActionResult 即可开始:

public class RssActionResult : ActionResult
{
    public SyndicationFeed Feed { get; set; }

    public override void ExecuteResult(ControllerContext context)
    {
        context.HttpContext.Response.ContentType = "application/rss+xml";

        Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(Feed);
        using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output))
        {
            rssFormatter.WriteTo(writer);
        }
    }
}

现在,在您的控制器操作中,您可以简单地返回以下内容:

return new RssActionResult() { Feed = myFeedInstance };

我的博客上有完整的示例 http://www.developerzen.com/2009/01/11/aspnet-mvc-rss-feed-action-result/

我同意哈克德的观点。我目前正在使用 MVC 框架实现我的网站/博客,并且我采用了为 RSS 创建新视图的简单方法:

<%@ Page ContentType="application/rss+xml" Language="C#" AutoEventWireup="true" CodeBehind="PostRSS.aspx.cs" Inherits="rr.web.Views.Blog.PostRSS" %><?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>ricky rosario's blog</title>
<link>http://<%= Request.Url.Host %></link>
<description>Blog RSS feed for rickyrosario.com</description>
<lastBuildDate><%= ViewData.Model.First().DatePublished.Value.ToUniversalTime().ToString("r") %></lastBuildDate>
<language>en-us</language>
<% foreach (Post p in ViewData.Model) { %>
    <item>
    <title><%= Html.Encode(p.Title) %></title>
    <link>http://<%= Request.Url.Host + Url.Action("ViewPostByName", new RouteValueDictionary(new { name = p.Name })) %></link>
    <guid>http://<%= Request.Url.Host + Url.Action("ViewPostByName", new RouteValueDictionary(new { name = p.Name })) %></guid>
    <pubDate><%= p.DatePublished.Value.ToUniversalTime().ToString("r") %></pubDate>
    <description><%= Html.Encode(p.Content) %></description>
    </item>
<% } %>
</channel>
</rss>

更多信息请查看(无耻插件) http://rickyrosario.com/blog/creating-an-rss-feed-in-asp-net-mvc

另一种疯狂的方法,但有其优点,是使用普通的 .aspx 视图来呈现 RSS。在您的操作方法中,只需设置适当的内容类型即可。这种方法的一个好处是很容易理解正在渲染的内容以及如何添加自定义元素(例如地理位置)。

话又说回来,列出的其他方法可能更好,我只是没有使用过它们。;)

我从 Eran Kampf 和 Scott Hanselman 视频(忘记了链接)那里得到了这个,所以它与这里的其他一些帖子略有不同,但希望有帮助,并准备好复制粘贴作为示例 rss feed。

来自我的博客

埃兰·坎普夫

using System;
using System.Collections.Generic;
using System.ServiceModel.Syndication;
using System.Web;
using System.Web.Mvc;
using System.Xml;

namespace MVC3JavaScript_3_2012.Rss
{
    public class RssFeed : FileResult
    {
        private Uri _currentUrl;
        private readonly string _title;
        private readonly string _description;
        private readonly List<SyndicationItem> _items;

        public RssFeed(string contentType, string title, string description, List<SyndicationItem> items)
            : base(contentType)
        {
            _title = title;
            _description = description;
            _items = items;
        }

        protected override void WriteFile(HttpResponseBase response)
        {
            var feed = new SyndicationFeed(title: this._title, description: _description, feedAlternateLink: _currentUrl,
                                           items: this._items);
            var formatter = new Rss20FeedFormatter(feed);
            using (var writer = XmlWriter.Create(response.Output))
            {
                formatter.WriteTo(writer);
            }
        }

        public override void ExecuteResult(ControllerContext context)
        {
            _currentUrl = context.RequestContext.HttpContext.Request.Url;
            base.ExecuteResult(context);
        }
    }
}

和控制器代码......

    [HttpGet]
public ActionResult RssFeed()
{
    var items = new List<SyndicationItem>();
    for (int i = 0; i < 20; i++)
    {
        var item = new SyndicationItem()
        {
            Id = Guid.NewGuid().ToString(),
            Title = SyndicationContent.CreatePlaintextContent(String.Format("My Title {0}", Guid.NewGuid())),
            Content = SyndicationContent.CreateHtmlContent("Content The stuff."),
            PublishDate = DateTime.Now
        };
        item.Links.Add(SyndicationLink.CreateAlternateLink(new Uri("http://www.google.com")));//Nothing alternate about it. It is the MAIN link for the item.
        items.Add(item);
    }

    return new RssFeed(title: "Greatness",
                       items: items,
                       contentType: "application/rss+xml",
                       description: String.Format("Sooper Dooper {0}", Guid.NewGuid()));

}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top