ASP.NET MVC RSS Feed Action Result

Guy wrote a post about rendering an RSS feed on ASP.NET MVC using custom feed model classes and a view that renders the feed XML.

There’s a better (shorter) way for achieving the same result while leveraging on the Syndication mechanism built into .NET’s WCF.
WCF exposes the SyndicationFeed, SyndicationItem, SyndicationPerson classes which represent our data model.
In order to render this model WCF also exposes the Atom10FeedFormatter, and RSS20FeedFormatter classes that can render the feed to a stream, so all we need to do is integrate that into the ASP.NET MVC pipeline.

The ASP.NET MVC framework introduces a concept of returning an ActionResult instance as the result of Controller Actions.
This ActionResult object indicates the result from an action (a view to render, a URL to redirect to, another action/route to execute, etc).

ASP.NET MVC ships with several Action Results:

  • ContentResult – Simply writes the returned data to the response.
  • EmptyResult – Returns an empty response.
  • HttpUnauthorizedResult – Returns Http 401 code for non authorized access.
  • JsonResult – Serializes the response to Json.
  • RedirectResult – Redirects to another Url.
  • RedirectToRouteResult – Redirects to another controller action.
  • ViewResultBase (abstract) – Renders an HTML content as a result.
    • PartialViewResult (inherits from ViewResultBase) – Renders a partial HTML response.
  • BinaryResult (abstract) – Returns a binary response.
    • BinaryStreamResult (inherits from BinaryResult) – Writes a binary stream as a result.

So basically, to return a feed result all we need to do is define our own ActionResult implementation by deriving from ActionResult:

public abstract class ActionResult
    protected ActionResult();

    public abstract void ExecuteResult(ControllerContext context);

All we need to do is override the ExecuteResult method and write our data model to the output http stream using RSS20FeedFormatter:

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

Now we can simply return RssActionResult as a result of our controller’s action.

Here’s a simple example:

public ActionResult Feed()
    SyndicationFeed feed =
        new SyndicationFeed("Test Feed",
                            "This is a test feed",
                            new Uri("http://Contoso/testfeed"),

    SyndicationItem item =
        new SyndicationItem("Test Item",
                            "This is the content for Test Item",
                            new Uri("http://Contoso/ItemOne"),

    List<SyndicationItem> items = new List<SyndicationItem>();
    feed.Items = items;

    return new RssActionResult() { Feed = feed };

… and that’s it!

A more elegant solution that leverages existing framework capabilities.

Update from Jan 16, 2011:

I’ve received an email from a reader saying the code above throws the following compile error:

Rss.ActionResult.ActionResult()’ must declare a body because it is not marked abstract, extern, or partial

I’m assuming this is due to some changes in recent ASP.NET MVC versions (or .NET?) but did not have time to test this myself as I’ve switched to linux development a while back I don’t feel like spending a whole day installing Windows and Visual Studio…

Anyway, I’ve sent this over to my friends at CodeValue and got back the following code (thanks Shay Friedman):

Related Posts

You may also like...

11 Responses

  1. Incredibly elegant!!

    So I just have to have .NET Framework 3.5, reference the System.ServiceModel.Web and I’m quite done?


  2. Very neat solution. Came in very handy for my site.


  3. That’s a very good post,
    I had a XmlWriterSettings for the XmlWriter to specify encoding :

    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Encoding = new UTF8Encoding(false);

    It Could be helpful 😉

    Thank you.

  4. Great solution!

    I was looking for the best way to do this, first stumbled upon the MVCContrib’s XmlResult and you took the next logical step, Exactly what I was looking for.

    Thanks a million!

  5. Thanks for the post! It came in very handy.

    I’d previously used the SyndicationFeed class in a WebForms app, but this was the first time I used it in an MVC app, and this article was completely clear and to the point.

  6. Good thing is we may use even Argotic Framework at the back end and it works great as well

  7. niaher says:

    Awesome post, just what I was looking for. Thanks.

  8. Great post. Needed something like this and thought I would google and up came you blog post. What would be great is support for some sort of adapter

  1. January 16, 2009

    […] wrote a post about rendering an RSS feed on ASP.NET MVC using custom feed model classes and a view that renders the feed XML. There’sa better […]

  2. October 6, 2009

    […] Action result is a very nice and helpful part of ASP.NET MVC that can act as a extensibility point as well. There are various derivations of ActionResult base class available in ASP.NET MVC, and there are some interesting extensions such as the one written for RSS feed generation. […]

  3. January 31, 2011

    […] Im creating an rss feed for my site and going with this way of building it: just not sure how do new feed get pushed to it though?eg for a site like suppose […]