Eran Kampf
Eran Kampf
3 min read

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))
        {
            rssFormatter.WriteTo(writer);
        }
    }
}

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"),
                            "TestFeedID",
                            DateTime.Now);

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

    List<SyndicationItem> items = new List<SyndicationItem>();
    items.Add(item);
    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):

gist:ekampf/781762