Awesome ASP.NET 2.0 RSS ToolKit Released
Dmitry on the ASP.NET Team posted an awesome RSS Toolkit for ASP.NET 2.0 on his blog earlier tonight. It provides a ton of great support for both consuming and exposing RSS from ASP.NET 2.0 applications, and ships with full source code.
March 26th, 2006 Update: Dmitry just updated the origional download here.
Some of the features/scenarios in the new RSS Toolkit include:
RSS Data-binding Control Support
You can now declaratively databind any ASP.NET control to the new RSSDataSource control provided in the toolkit. For example, to build a simple blog post aggregator for my blog you could follow the below steps with the free Visual Web Developer tool:
1) Create a new blank page
2) Drag/Drop the standard ASP.NET DataList control on the page, choose the “New DataSource” option from the smart-task:
3) Select the RSSDataSource control to bind to (note: this will automatically be populated in the datasource dialog window if the RSSDataSource control has been added to the VS toolbox):
4) In the RSSDataSource dialog that pops-up, type in the URL to the RSS feed you want to databind to:
5) The RSSDataSource control will then request the schema for the RSS feed and expose it to VS – which will then populate the <asp:datalist> control with an <itemTemplate> that is automatically populated with data-binding expressions for each of the RSS schema fields from the remote RSS source:
6) You can then switch into template editing mode in the WYSIWYG designer, or go into source-view to remove some of the fields and just leave the RSS fields you want to bind to. Here is a sample page with a DataList template that binds to the link and title RSS item fields:
<%@ Register Assembly="RssToolkit" Namespace="RssToolkit" TagPrefix="RssToolkit" %>
<html>
<head runat="server">
<title>ScottGu Feed</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>ScottGu Blog Feed</h2>
<asp:DataList ID="DataList1" runat="server" CellPadding="4" DataSourceID="RssDataSource1" ForeColor="#333333">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("link") %>' Text='<%# Eval("title") %>'></asp:HyperLink>
</ItemTemplate>
</asp:DataList>
<RssToolkit:rssdatasource id="RssDataSource1" runat="server" url="http://weblogs.asp.net/scottgu/Rss.aspx">
</RssToolkit:rssdatasource>
</div>
</form>
</body>
</html>
7) I can then run the page and get this output:
As you can see above, the DataList is now bound to the data returned from the remote RSS feed on my site.
What is really cool is that the RSSDataSource control will cache the RSS data to avoid hitting it remotely on each request – so that you can efficiently add this type of blog aggregation functionality to your site and not have to worry about your performance slowing down.
Strongly Typed RSS API Support
The RSS Toolkit also adds support for creating a strongly-typed object model over a remote RSS feed. This allows you to program against RSS data in a clean, easy way. You can do this one of two ways:
1) Using the rsssdl.exe utility that ships with the toolkit. This is a command-line tool that generates a strongly-typed class library based on a provided RSS URL feed (sort of like wsdl.exe does for web-services). For example, I could use it against my ScottGu RSS blog feed like so to generate a Scottgu.cs file containing C# classes that provide a strongly-typed object model around it:
I could then add the generated “scottgu.cs” file to my project and instantiate and use the class framework to programmatically databind the RSS channel’s items against a GridView (note that each of the Items is strongly typed as a "ScottGuChannelItem" -- and has a strongly-typed object model based on the schema of the RSS feed):
ScottGuChannel channel = ScottGuChannel.LoadChannel();
GridView1.DataSource = channel.Items;
GridView1.DataBind();
or alternatively:
2) The RSS toolkit also ships with a new ASP.NET 2.0 build-provider that allows you to declaratively add .rssdl file definitions in your app_code directory. For example like so:
<rssdl>
<rss name="ScottGu" url="http://weblogs.asp.net/scottgu/rss.aspx" />
</rssdl>
This will then automatically generate and build a RSS channel proxy into your project for you to use (no command line tool usage required).
RSS Publishing Support
The RSS Toolkit also adds support for easily and efficiently publishing RSS feeds from an ASP.NET 2.0 application. For example, to build an RSS feed on your site with it you can now do the following:
Step 1) Add a .rss sample definition file to your app_code directory that provides some sample schema for what you want the RSS feed to look like. For example, a Sample5.rss file like so:
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Sample Channel</title>
<link>~/scenario5.aspx</link>
<description>Sample Channel Example</description>
<ttl>10</ttl>
<name></name>
<user></user>
<item>
<title></title>
<description></description>
<link></link>
</item>
</channel>
</rss>
This will then create a strongly typed object-model for the RSS schema, as well as build a default RSSHandlerBaseClass for you to use to publish it within an ASP.NET .ashx handler file.
Step 2) Create a Sample5.ashx handler file in VS 2005 or Visual Web Developer to expose the channel information. You can sub-class the RSSHandlerBaseClass automatically defined from the sample schema above to publish the RSS data in a strongly typed way:
<%@ WebHandler Language="C#" Class="scenario5" %>
using System;
using System.Web;
public class scenario5 : Sample5HttpHandlerBase {
protected override void PopulateChannel(string channelName, string userName) {
Channel.Name = channelName;
Channel.Items.Add(
new Sample5Item("Scenario1",
"Consuming RSS feed using RssDataSource",
"~/scenario1.aspx"));
Channel.Items.Add(
new Sample5Item("Scenario2",
"Consuming RSS feed using ObjectDataSource",
"~/scenario2.aspx"));
Channel.Items.Add(
new Sample5Item("Scenario3",
"Consuming RSS feed programmatically using strongly typed classes",
"~/scenario3.aspx"));
Channel.Items.Add(
new Sample5Item("Scenario4",
"Consuming RSS feed programmatically using late bound classes",
"~/scenario4.aspx"));
}
}
Note that all operations within the RSS handler are strongly typed (meaning you’ll get intellisense support, and a compile error if you mistype something).
Step 3) When you hit the URL to the above handler you’ll then get back an RSS XML feed that you can easily subscribe to:
Note above how the channel item paths are fully qualified in the output RSS XML, but are using the “~” syntax in the .ashx handler. The RSS toolkit adds a nice feature which is the ability for a developer to use the “~” syntax to automatically fix up fully qualified path statements from the channel handler (that way the paths will be correct regardless of whatever server or virtual directory hierarchy the application is deployed on).
Personalized RSS Feed Integration with ASP.NET Authentication System
One of the tricks people are doing these days with RSS is to enable people to subscribe to feeds that are personalized for them. They often do this by adding a query-string parameter to the RSS feed that contains a unique identifier to map the URL to a specific account name on the server (allowing a user for example to publish and subscribe to their private calendar or task-list or contact-list information).
The RSS Toolkit allows you to easily build these types of apps and integrate them with the ASP.NET membership and authentication system. Note how in the handler above there is a “username” parameter to the PopulateChannel method. ASP.NET will look for an encrypted username value on the incoming RSS feed URL, and automatically decrypt and pass the username to this method when it is supplied (you could then write code within this method to hit a database and pull back only the items for that particular user).
To embed the encrypted user-name within the RSS feed URL, you can either get the value via an API call, or generate the RSS URL link using the new declarative RSSHyperlink control and setting the IncludeUserName=”true” property to true:
<RssToolKit:RssHyperLink runat="server" IncludeUserName="True" NavigateUrl="~/scenario6.ashx">Personalized RSS Feed Sample</RssToolKit:RssHyperLink>
When a user clicks on this feed it will include their username encrypted within the URL. When they copy/paste the feed link into a blog reader (for example: to subscribe to it), it will then remember the user’s identity and allow them to always have a personalized view of the data.
Automatic Caching of RSS Feeds
The RSS Toolkit automatically includes built-in caching logic so that RSS feed data is cached locally instead of fetched each time it is accessed/used. The RSS Toolkit supports caching this information both in-memory, as well as persistently to disk (for example: to survive worker process restarts or application resets).
The benefit of this built-in caching integration is that you can efficiently subscribe and pull RSS data from a variety of remote sources without having to worry about your server blocking on remote network calls all the time. Note that the RSS toolkit by default uses an RSS channel’s TTL setting to control the cache duration semantics for you.
Summary
The new ASP.NET RSS Toolkit provides a ton of cool RSS functionality to take advantage of, and allows you to easily integrate RSS within both your sites and applications. RSS is no longer just about blogging – it is now a super common data format for people to publish and subscribe to lots of different types of information (for example: check out this sample I built using the December CTP build of Atlas for a sample “Ajax Task List App” that publishes to-do items using RSS).
You can download the RSS Toolkit from here. Best of all it is free, and all of the Toolkit’s source code is included with the sample. It includes a number of great scenario samples that you can walkthrough to quickly learn how to use it.
The RSS Toolkit has been written to work in medium trust security scenarios – so you can run it on shared hosting servers (note: to consume remote RSS feeds the hosted server will need to allow remote out-bound http requests). Click here to learn more about how to find a hoster that provide inexpensive ASP.NET 2.0 and SQL hosting.
Special thanks go to Dmitry for building this RSS Toolkit in his spare time. If you download and use it please make sure to thank him through his blog. J
Hope this helps,
Scott
P.S. In case you missed it in December, Dmitry also built and published a great disk-based output caching module for ASP.NET 2.0 as well. You can download it here.