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.

Related Posts

Similar Posts:

Tags: , ,


8 Comments to “ASP.NET MVC RSS Feed Action Result”

  1. Shared Tutorials » Blog Archive » ASP.NET MVC RSS Feed Action Result | DeveloperZen | January 16th, 2009 at 7:55 am

    [...] 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. Andrei Rinea | January 29th, 2009 at 2:47 am

    Incredibly elegant!!

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

    Wow…

  3. Graeme Finn | July 7th, 2009 at 8:52 am

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

    Thanks.

  4. Sitemap Action Result for ASP.NET MVC | Keyvan Nayyeri | October 6th, 2009 at 10:17 pm

    [...] 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. [...]

  5. Thomas Cortes | December 11th, 2009 at 1:43 am

    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.

  6. Dean Poulin | December 15th, 2009 at 11:03 pm

    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!

  7. Chris Shipman | February 25th, 2010 at 11:28 pm

    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.

  8. Asad Ali Butt | March 14th, 2010 at 10:07 pm

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

Leave a Comment


Add a comment on FriendFeed