Aug 17

Creating an RSS Feed in ASP.NET MVC

A short tutorial of a simple implementation of an RSS feed in an ASP.NET MVC site

This blog was missing the third most important feature of a blog: RSS. (The first two most important features are posts and comments because a blog without comments is not a blog. I am still waiting for comments on this blog but I am sure it will take time.)

There are probably many ultra fancy ways to create an RSS feed using existing libraries for .Net. For example, one could create a new ActionResult that returned RSS: RssResult (analogous to JsonResult for returning JSON). This isn't rocket science, so I decided to keep it simple and just create a new View for RSS.

The first step was to create a new Action in my Blog Controller that retrieves the latest (12 in my case) posts and passes them on to the View:

public ActionResult PostFeed(string type) {
    if (type.ToLower().Equals("rss")) {
        return View("PostRSS", _blogService.GetPosts(0,12));
    } else {
        return RedirectToAction("Index", "Home");

The (syndication) type is passed in so that I can easily add Atom or any other format in the future. If an invalid type is passed in then I just redirect the user to the home page. Maybe I should redirect to the RSS feed instead?

The next step is to create the View (PostRSS.aspx), which is simply a template for the RSS XML with a foreach loop to go through the posts. Note that I had to put the <?xml ... ?> declaration on the same line and to the right of the <%@ Page ... %>. Otherwise, a blank line is rendered at the top of the page and the XML becomes invalid :-|.

<%@ Page ContentType="application/rss+xml" Language="C#" AutoEventWireup="true" CodeBehind="PostRSS.aspx.cs" Inherits="rr.web.Views.Blog.PostRSS" %><?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<title>ricky rosario's blog</title>
<link>http://<%= Request.Url.Host %></link>
<description>Blog RSS feed for</description>
<lastBuildDate><%= ViewData.Model.First().DatePublished.Value.ToUniversalTime().ToString("r") %></lastBuildDate>
<% foreach (Post p in ViewData.Model) { %>
    <title><%= Html.Encode(p.Title) %></title>
    <link>http://<%= Request.Url.Host + Url.Action("ViewPostByName", new RouteValueDictionary(new { name = p.Name })) %></link>
    <guid>http://<%= Request.Url.Host + Url.Action("ViewPostByName", new RouteValueDictionary(new { name = p.Name })) %></guid>
    <pubDate><%= p.DatePublished.Value.ToUniversalTime().ToString("r") %></pubDate>
    <description><%= Html.Encode(p.Content) %></description>
<% } %>

After this you can add a route if you want to customize your feed URL to be something like

    new { controller = "Blog", action = "PostFeed", type = "rss" }

That's it! Most bloggers seem to use feedburner to manage their RSS feeds, so I did that as well and it was very straightforward. They provide a bunch of HTML snippets to advertise your feed.

BTW, writing this post is making me realize how bad I need to integrate some kind of editor for creating blog posts, rather than typing into a textarea and manually encoding the xml code above. I also need to pick a syntax highlighter and plug it in for easier reading. Nobody is reading yet though, so it's ok ;).


Powered by my hackings on Django-Mingus a Django project, PostgreSQL, memcached, nginx, Apache + mod_wsgi, Ubuntu, Rackspace Cloud,...

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

html5 | top