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:
Listing 4
<?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:
Listing 5
<%@ 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:
Figure 8
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).