How would you reccommend handling RSS Feeds in ASP.NET MVC? Using a third party library? Using the RSS stuff in the BCL? Just making an RSS view that renders the XML? Or something completely different?
-
1Just to update this question as of 18 months following last edit - It seemed reasonable to question 'have things changed with iterations to both .net and mvc that would change how we approach the problem of feed management'? The consensus (via a different SO thread) is that, 'No fundamental changes - this remains your best set of alternatives.' – justSteve Mar 07 '11 at 15:35
-
Here's a follow up post that takes the RssActionResult idea a bit further with a generalized SyndicationAction result class as well as a 304 NotModified conditional get filter. http://www.58bits.com/blog/ASPNET-MVC-304-Not-Modified-Filter-For-Syndication-Content.aspx – Blue Waters Jul 29 '09 at 05:36
-
Using RssToolkit you just need to have a single .ashx file in your project to generate RSS feed. Then you can rewrite its URL to a friendly one. I think there is not anything against MVC in this approach. – Mahdi Taghizadeh Nov 12 '08 at 19:43
-
bad suggestion for ASP.NET MVC. – tugberk Dec 18 '11 at 18:14
-
I've wrote an RssResult which you can have a look at if you like. It should meet your requirements [http://www.wduffy.co.uk/blog/rssresult-aspnet-mvc-rss-actionresult/](http://www.wduffy.co.uk/blog/rssresult-aspnet-mvc-rss-actionresult/) – William Oct 29 '09 at 11:42
-
I recently wrote an article on [how to create RSS feeds in asp.net mvc](http://tech.pro/tutorial/1117/generating-rss-feed-actions-in-aspnet-mvc) which should also do what you are looking for – Leland Richardson Feb 22 '13 at 05:16
5 Answers
The .NET framework exposes classes that handle syndation: SyndicationFeed etc. So instead of doing the rendering yourself or using some other suggested RSS library why not let the framework take care of it?
Basically you just need the following custom ActionResult and you're ready to go:
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);
}
}
}
Now in your controller action you can simple return the following:
return new RssActionResult() { Feed = myFeedInstance };
There's a full sample on my blog at http://www.developerzen.com/2009/01/11/aspnet-mvc-rss-feed-action-result/

- 111,873
- 86
- 233
- 325

- 8,928
- 8
- 49
- 47
Here is what I recommend:
- Create a class called RssResult that inherits off the abstract base class ActionResult.
- Override the ExecuteResult method.
- ExecuteResult has the ControllerContext passed to it by the caller and with this you can get the data and content type.
Once you change the content type to rss, you will want to serialize the data to RSS (using your own code or another library) and write to the response.
Create an action on a controller that you want to return rss and set the return type as RssResult. Grab the data from your model based on what you want to return.
Then any request to this action will receive rss of whatever data you choose.
That is probably the quickest and reusable way of returning rss has a response to a request in ASP.NET MVC.

- 18,202
- 3
- 54
- 70
-
10Hanselman has a [similar solution](http://live.visitmix.com/MIX10/Sessions/FT07) (video: starting around 41m) where he inherits from FileResult. By doing so, you can have your class's constructor call `base("application/rss+xml")` and avoid steps 3 and 4. He does override ExecuteResult, but it isn't vital. He also shortcuts a lot of typically-homespun code and uses the 3.5+ features of `SyndicateItem`, `SyndicateFeed`, and `Rss20FeedFormatter`. – patridge Jul 21 '10 at 22:04
-
@Dale: is it possible to write to the response when you want to output to feed to a partial view? Thanks. – Christian Aug 08 '11 at 07:40
-
1Updated link of [Hanselman's video](http://channel9.msdn.com/Events/MIX/MIX10/FT07) from my prior comment. – patridge Jul 02 '12 at 16:54
I agree with Haacked. I am currently implementing my site/blog using the MVC framework and I went with the simple approach of creating a new View for 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>
For more information, check out (shameless plug) http://rickyrosario.com/blog/creating-an-rss-feed-in-asp-net-mvc

- 5,365
- 2
- 32
- 30
-
4for Razor use: @model PageModel @{Response.ContentType = "application/rss+xml"; } – Anthony Johnston Jan 21 '11 at 16:11
-
2What overhead? you mean the fact that you're writing less code to accomplish the same thing in a more readable way? – Paul Mar 06 '11 at 19:19
Another crazy approach, but has its advantage, is to use a normal .aspx view to render the RSS. In your action method, just set the appropriate content type. The one benefit of this approach is it is easy to understand what is being rendered and how to add custom elements such as geolocation.
Then again, the other approaches listed might be better, I just haven't used them. ;)

- 58,045
- 14
- 90
- 114
-
How can you ensure the XML is valid doing this way? It would be nice if the view rendering was decoupled from an incoming web request, to make XML views, or email templates like done ruby on rails possible. – Paco Nov 12 '08 at 20:28
-
Rather than using a view engine, you could create an RssResult that derives from ActionResult. We do this with the JsonResult which serializes the object to JSON. In your case, you'd find some serializer (I think WCF has one) that serializes to RSS. – Haacked Nov 19 '08 at 08:49
-
3@Haacked: The world is full of invalid RSS XML generated by a templating system. Please don't add to the mess! Ricky, HTML encoding != XML encoding. – Brad Wilson Aug 17 '08 at 16:01
-
Below is the documentation of Html Encode from MSDN: > Due to current implementation details, this function can be used as an xmlEncode function. Currently, all named entities used by this function are also xml predefined named entities. They are < > " & encoded as < > " and &. Other entities are decimal-encoded like . [http://msdn.microsoft.com/en-us/library/73z22y6h.aspx](http://msdn.microsoft.com/en-us/library/73z22y6h.aspx) – Ricky Aug 17 '08 at 22:54
I got this from Eran Kampf and a Scott Hanselman vid (forgot the link) so it's only slightly different from some other posts here, but hopefully helpful and copy paste ready as an example 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);
}
}
}
And the Controller Code....
[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()));
}