ASP.NET MVC 中的 RSS 源

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

44300 次浏览

以下是我的建议:

  1. 创建一个名为 RssResult 的类 继承了抽象基类 行动结果。
  2. 重写 ExecuteResult 方法。
  3. ExecuteResult 具有调用方传递给它的 ControllerContext,通过它可以获得数据和内容类型。
  4. 一旦您将内容类型更改为 RSS,您将希望将数据序列化为 RSS (使用您自己的代码或另一个库)并写入响应。

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

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

That is probably the quickest and reusable way of returning rss has a response to a request in ASP.NET MVC.

另一个疯狂的做法,但有其优点,是使用一个正常的。Aspx 视图来呈现 RSS。在操作方法中,只需设置适当的内容类型。这种方法的一个好处是很容易理解正在呈现的内容以及如何添加诸如地理定位之类的自定义元素。

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

我同意哈克德的观点。我目前正在使用 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

NET 框架公开了处理聚合的类: SyndicationFeed 等等。 So instead of doing the rendering yourself or using some other suggested RSS library why not let the framework take care of it?

基本上,你只需要下面的自定义 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/上有一个完整的样本

我从 Eran Kampf 和 Scott Hanselman 那里得到了这个视频(忘记了链接) ,所以它和这里的其他帖子只有一点点不同,但是希望它有帮助,并且可以作为一个示例 rss 提要复制粘贴。

我博客上的

Eran Kampf

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()));


}