Wednesday, February 22, 2012

Showing RSS items (from your blog) on your ASP.NET MVC homepage

The title of this post could also have been: "Reading a RSS feed (from your blog) in .NET".
The reason is that the RSS classes that I will be describing are available in all types of .NET applications, not only the web-based onces. But I think displaying a blog item on your home page is a nice and relatively simple example of consuming RSS feeds in .NET.
There actually is built-in RSS support in .NET since the release of framework version 3.5. I have not been aware of that support for some time and occasionally used the third-party library RSS.NET. It turns out that we do not need a separate library any longer.

What does the C# code to read RSS feeds look like? Here is an example MVC controller that refers to the SyndicationFeed and SyndicationItem classes in its Index action method.
using System.Web.Mvc;
using System.ServiceModel.Syndication;
using System.Xml;
using System.Linq;

namespace Antrix.Web.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            const string feedUrl =
                "http://buildingwebapps.blogspot.com/feeds/posts/default?alt=rss";
            SyndicationFeed feed = null;
            using (XmlReader reader = XmlReader.Create(feedUrl))
            {
                feed = SyndicationFeed.Load(reader);
            }
            if (feed != null)
            {
                SyndicationItem item = feed.Items.First<SyndicationItem>();
                ViewBag.RssItem = item;
            }

            return View();
        }

        public ActionResult About()
        {
            return View();
        }
    }
}

The Index method reads a feed of the blog that you are reading right now. It puts the first item (which we assume represents the last post that has been published) in the ViewBag. Normally I use model classes to refer to data within my MVC views, but I want to keep things simple.

Here is the default homepage view that comes with ASP.NET MVC 3 projects in Visual Studio 2010. I have added code to show the title and summary of the last post of this blog, and a link to the full post.
@{
    ViewBag.Title = "Home Page";
}

<h2>@ViewBag.Message</h2>
<p>
    To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
</p>
<div>
    From our blog:
    <div style="font-weight: bold; padding: 10px">@ViewBag.RssItem.Title.Text</div>
    <div style="clear: both; padding: 10px">@Html.Raw(@ViewBag.RssItem.Summary.Text)</div>
    <div style="clear: both; padding: 10px"><a href="@ViewBag.RssItem.Links[0].Uri" target="_blank">Show full post</a></div>
</div>
Using the SyndicationFeed and SyndicationItem classes it becomes rather easy to create your own web-based (or Windows-based, if you prefer) RSS reader. Who will build one that outclasses the others?

1 comment: