RSS Feeds in ASP.NET MVC

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


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


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/


Here is what I recommend:

  1. Create a class called RssResult that inherits off the abstract base class ActionResult.
  2. Override the ExecuteResult method.
  3. ExecuteResult has the ControllerContext passed to it by the caller and with this you can get the data and content type.
  4. 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.

  5. 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.

  6. 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.