ASP.Net MVC RSS Feed Action Result

11 בינואר 2009

4 תגובות

ASP.Net MVC RSS Feed Action Result

ASP.Net MVC RSS Feed Action Result ASP.Net MVC Controller Actions usually returns an object that inherits from the ActionResult class.

public abstract class ActionResult
  public abstract void ExecuteResult(ControllerContext context);

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.

If you want to return a RSS Feed as a result, here is what you should do.

Create an Object Model for the RSS Feed

In this samples I created an object model from scratch, but one should use an existing object model from his site.

public class Feed
  public Feed() { Items = new List<FeedItem>(); }
  public string Title { get; set; }
  public string Description { get; set; }
  public string Url { get; set; }
  public string Language { get; set; }
  public List<FeedItem> Items { get; set; }
public class FeedItem
  public FeedItem() { Tags = new List<string>(); }
  public string Creator { get; set; }
  public string Title { get; set; }
  public string Description { get; set; }
  public string Url { get; set; }
  public DateTime Published { get; set; }
  public List<string> Tags { get; set; }

With the above object model, a simple test data can be created with the following method:

private Feed GetFeedData()
  Feed feed = new Feed {
    Title = "Guy Burstein's Blog",
    Description = "Description of Guy Burstein's Blog",
    Language = "en",
    Url = "",
    Items = {
      new FeedItem { 
        Title = "Post 1", 
        Creator = "Guy Burstein", 
        Description = "Body of post 1", 
        Url = "",
        Published = DateTime.Now,
        Tags = { "Tag1", "Tag2", "Tag3" }
      new FeedItem { 
        Title = "Post 2", 
        Creator = "Guy Burstein", 
        Description = "Body of post 2", 
        Url = "",
        Published = DateTime.Now - new TimeSpan(1, 0, 0),
        Tags = { "Tag2", "Tag4" }
  return feed;

Create the RSS Feed Template View

Since the output RSS Feed has its format, we can use the existing View engine to render it. Instead of rendering an HTML, this view will render the RSS feed.

Create a new view called Rss.aspx under the Views\Shared folder. Next, in order to make this view worked with strongly typed model, go to the code-behind file (Rss.Aspx.cs) and let the view inherit from ViewPage<T> where T is the above Feed class.

public partial class Rss : ViewPage<Feed>

Now, edit the view’s markup and create the template of the RSS Feed, like the following:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Rss.aspx.cs" Inherits="RSSSample.Views.Shared.Rss" %>
<rss version="2.0" xmlns:dc="">
        <title><%=Html.Encode(ViewData.Model.Title) %></title>
        <description><%=ViewData.Model.Description %></description>
       <%foreach (FeedItem item in ViewData.Model.Items)
            <link><%=item.Url %></link>
            <pubDate><%=item.Published.ToString("R") %></pubDate>
            <% foreach (string tag in item.Tags)
               { %>
                <category><%=tag %></category>
            <% } %>
          } %>

Notice that the access to each property of the feed is strongly typed using ViewData.Model.<Property>, and that the values are encoded using the Html helper of the view.

Implementing the Controller to render RSS Action Result

In a controller of our choice, we can add a method that will return the results as RSS Feed.

public ActionResult Rss()
  // Get Feed Data
  Feed f = GetFeedData();
  return View(f);      

This method gets the feed object model populated, and renders the above view passing the feed as the model.

ASP.Net MVC RSS Feed Action Result


הוסף תגובה
facebook linkedin twitter email

4 תגובות

  1. ekampf11 בינואר 2009 ב 17:13

    Hi Guy,
    An easier solution would be using WCF syndication constructs (SyndicationFeed class) and creating your own ActionResult derived class.
    Override the ExecuteResult method and then you can use Atom10FeedFormatter\RSS20FeedFormatter to serialize your feed directly into context.HttpContext.Response.Output.


  2. שי יעקובי25 בינואר 2009 ב 1:29

    Hi Guy.
    It's would add on the page level: ContentType="application/rss+xml".
    I recommend to validate the rss output with this validator:

  3. SMS_quiewity5 באפריל 2009 ב 12:59

    hello, where are you out a such DIZ?

  4. Anthony Bouch29 ביולי 2009 ב 8:44

    Here's a follow-up post that takes the RssActionResult idea a bit further with a generalized SyndicationActionResult as well as a 304 NotModified filter for ASP.Net MVC and syndication content.

Comments are closed.