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:
- Developing a Robust Data Driven UI Using WPF – The DataModel
- Is Windows Live Still Alive?
- Developing a Robust Data Driven UI Using WPF – Stock DataModel Sample
- Migrating from dasBlog to Wordpress
- Developing a Robust Data Driven UI Using WPF – An Overdue Summary (and full source code)
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…
Very neat solution. Came in very handy for my site.
Thanks.
[...] 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. [...]
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.
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!
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.
Good thing is we may use even Argotic Framework at the back end and it works great as well