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 }; }
A more elegant solution that leverages existing framework capabilities.
Related Posts
- RSS Feed with the new ASP.NET MVC Framework (Brad Abrams)
- ASP.NET MVC RSS Feed Action Result (Guy Burstein)
- RSS Feeds in ASP.NET MVC – StackOverflow
Similar Posts:
- Creating an Office-Like MVC UI Architecture
- Developing a Robust Data Driven UI Using WPF – The DataModel
- Lets Socialize
- Is Windows Live Still Alive?
- Developing a Robust Data Driven UI Using WPF – Stock DataModel Sample
Tags: ActionResult, aspnetmvc, RssActionResult













[...] 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 [...]
Incredibly elegant!!
So I just have to have .NET Framework 3.5, reference the System.ServiceModel.Web and I’m quite done?
Wow…