Monday, September 8, 2008

A Custom ASP.NET Server Control for Displaying RSS Feeds

By Scott Mitchell

When creating this recommended blogs page, I decided rather than merely listing links to the various blogs, I'd like to also show the title of each recommended blog's most recent entries. This task is not too difficult to accomplish because blogs typically provide an RSS syndication feed. RSS is a standard that spells out a format for syndicated content in an XML format. (For the specifics of the format, see the RSS 2.0 specification.) There are a number of programs, called aggregators that are designed specifically to organize syndicated content. Some free aggregators build using the .NET Framework include SharpReader and RssBandit. You can also easily build your own aggregator, as I described in this article: Creating an Online RSS News Aggregator with ASP.NET.

Initially I decided to just use the techniques described in an earlier 4Guys article, Consuming an RSS Feed with ASP.NET, which demonstrates how to display the items in an RSS feed in a data Web control. However, the code to implement this technique, while only a few lines of code long, seemed overly complex for the simple task I was after. Furthermore, the code, as-is, does not employ any caching of the RSS data, which not only leads to longer load times on my end (since the content must be grabbed from a remote Web server on each page load), but is also bad netiquette since it greatly increases the bandwidth sucked down from the server hosting the blog. What I wanted to be able to do was the following: add a Web control to my ASP.NET Web page and with one or two lines of code, at most, have the control display the items from an RSS feed. Too, the control should cache the downloaded RSS feed for a developer-specified number of minutes.

To solve this problem I decided to create a custom ASP.NET server control, which I call RssFeed. RssFeed is an open-source control, with complete source code available for download and modification at the RssFeed GotDotNet Workspace. This article explains how to obtain, install, and use RssFeed in an ASP.NET Web page. For technical information about RssFeed and its source code, be sure to consult the RssFeed Project Page.

Downloading RssFeed
You can download the code and/or a compiled assembly for the RssFeed server control by visiting the Releases Archives for the RssFeed GotDotNet Workspace. From the archives, you can download the entire source code or a pre-compiled assembly (.dll file). (Note: the compiled assembly was compiled using Visual Studio .NET 2003, and is therefore targeted for the .NET Framework Version 1.1. If you need to use RssFeed for .NET Framework 1.0, you will need to download the source code and compile it through Visual Studio .NET 2002.)

If you download the source code, before using RssFeed in an ASP.NET Web application you'll need to compile the source code. If you have Visual Studio .NET, this task is simple - simply double-click the skmRss.csproj file in the downloaded code files, which will open Visual Studio .NET. Next, go to the Build menu and choose Build Solution. That's it! This will create the compiled assembly - skmRss.dll. (If you are going to be using RssFeed in an ASP.NET Web application on a Web server running the .NET Framework Version 1.1, you can just download the pre-compiled assembly instead, if you want to skip the step of compilation yourself...)

Using RssFeed in an ASP.NET Web Application
The first step to using RssFeed in an ASP.NET Web application is to add the assembly, skmRss.dll, to the Web application. If you are building your ASP.NET Web application using Visual Studio .NET you can accomplish this by adding the assembly to the References folder. From the Solution Explorer, right-click on the References folder, Browse to the skmRss.dll file, and click OK. If you are not using Visual Studio .NET, simply copy the skmRss.dll file to the Web application's /bin directory.

If you are using Visual Studio .NET you can, at this point, add RssFeed to the Toolbox. To accomplish this, right-click on the Toolbox in Visual Studio .NET and select Add/Remove Toolbox Items (the exact wording differs between Visual Studio .NET 2002 and 2003...). Browse to the skmRss.dll file and click OK. This will add a control called RssFeed to the Toolbox. To add the RssFeed control to an ASP.NET Web page simply drag the control from the Toolbox onto an ASP.NET Web Form's Design tab. As you can see in the screenshot below, RssFeed provides a rich design-time experience with Visual Studio .NET.


ASP.Net Feeds