DCSIMG
ASP.Net MVC RSS Feed Action Result - Guy Burstein's Blog

Guy Burstein's Blog

Developer Evangelist @ Microsoft

News

Guy Burstein The Bu

Disclaimer
Postings are provided 'As Is' with no warranties and confer no rights.

Guy Burstein LinkedIn Profile

TwitterCounter for @bursteg

Links

Articles

Blogs I Read

ASP.Net MVC RSS Feed Action Result

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 = "blog.bursteg.net",
    Items = {
      new FeedItem { 
        Title = "Post 1", 
        Creator = "Guy Burstein", 
        Description = "Body of post 1", 
        Url = "blogs.bursteg.net/archive/1",
        Published = DateTime.Now,
        Tags = { "Tag1", "Tag2", "Tag3" }
      },
      new FeedItem { 
        Title = "Post 2", 
        Creator = "Guy Burstein", 
        Description = "Body of post 2", 
        Url = "blogs.bursteg.net/archive/2",
        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="http://purl.org/dc/elements/1.1/">
    <channel>
        <title><%=Html.Encode(ViewData.Model.Title) %></title>
        <description><%=ViewData.Model.Description %></description>
        <link><%=ViewData.Model.Url%></link>
        <language><%=ViewData.Model.Language%></language>
 
       <%foreach (FeedItem item in ViewData.Model.Items)
         {%>
        <item>
            <dc:creator><%=Html.Encode(item.Creator)%></dc:creator>
            <title><%=Html.Encode(item.Title)%></title>
            <description><%=Html.Encode(item.Description)%></description>
            <link><%=item.Url %></link>
            <pubDate><%=item.Published.ToString("R") %></pubDate>
            <% foreach (string tag in item.Tags)
               { %>
                <category><%=tag %></category>
            <% } %>
        </item>
        <%
          } %>
    </channel>
</rss>

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

Enjoy!

Comments

ekampf said:

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.

Regards,

Eran

# January 11, 2009 5:13 PM

ASP.NET MVC RSS Feed Action Result | DeveloperZen said:

Pingback from  ASP.NET MVC RSS Feed Action Result | DeveloperZen

# January 11, 2009 5:44 PM

ASP.NET MVC Archived Blog Posts, Page 1 said:

Pingback from  ASP.NET MVC Archived Blog Posts, Page 1

# January 12, 2009 6:19 AM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# January 12, 2009 9:45 AM

ASP.Net MVC RSS Feed Action Result - Guy Burstein's Blog said:

Pingback from  ASP.Net MVC RSS Feed Action Result - Guy Burstein&#39;s Blog

# January 13, 2009 3:16 PM

שי יעקובי said:

Hi Guy.

It's would add on the page level: ContentType="application/rss+xml".

I recommend to validate the rss output with this validator:

http://validator.w3.org/feed/

# January 25, 2009 1:29 AM

SMS_quiewity said:

hello, where are you out a such DIZ?

# April 5, 2009 12:59 PM

Anthony Bouch said:

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.

www.58bits.com/.../ASPNET-MVC-304-Not-Modified-Filter-For-Syndication-Content.aspx

# July 29, 2009 8:44 AM

ASP.Net MVC RSS Feed Action Result | MIKE said:

Pingback from  ASP.Net MVC RSS Feed Action Result | MIKE

# September 23, 2009 1:17 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: