Now a day’s RSS is an emerging web media. RSS is useful to distribute up-to-date web content from one web site to thousands of other web sites around the world. This allows fast browsing for news and updates.
What is RSS?
RSS (Rich Site Summary) is a format for delivering regularly changing web content. Many news-related sites, weblogs and other online publishers syndicate their content as an RSS Feed to whoever wants it. Generally lot of feed readers are available (e.g.: reader.google.com…). If people add their feed to the reader, this will serve the latest up-to-date content.
Without RSS, users will have to check your site daily for new updates. This may be too time-consuming, difficult for many users. With an RSS feed (RSS is often called a News feed or RSS feed) they can check your site faster using an RSS aggregator (a site or program that gathers and sorts out RSS feeds).
What is Generic handler in ASP.NET?
Before going to know about generic handlers we should have knowledge on HTTP handlers.By default ASP.NET includes several classes, which are capable of handling requests in the most common ways. For example the Page Class handles the requests and returning meaningful user interface (HTML). The Service class interprets incoming query string as method calls and processes them accordingly. Some time programmers may needed to handle the request in a way not already provided through the System.Web.UI.Page or the System.Web.Services.Service classes. In that case ASP.Net suggest the Custom handlers solution.
HTTP handlers are simply classes that implement IHttpHandler. Handlers are listed inside web.config. As with the HTTP modules, ASP.NET comes out of the box with several HTTP Handlers already. ASP.NET comes with the below HTTP handlers already registered in the master web.config file (which resides alongside machine.config in the main configuration directory).
<httpHandlers> <add verb="*" path="*.rules" type="System.Web.HttpForbiddenHandler" validate="true"/> <add verb="*" path="*.xoml" type="System.ServiceModel.Activation.HttpHandler, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" validate="false"/> <add path="*.svc" verb="*" type="System.ServiceModel.Activation.HttpHandler, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" validate="false"/> <add path="trace.axd" verb="*" type="System.Web.Handlers.TraceHandler" validate="True"/> <add path="WebResource.axd" verb="GET" type="System.Web.Handlers.AssemblyResourceLoader" validate="True"/> <add path="*.axd" verb="*" type="System.Web.HttpNotFoundHandler" validate="True"/> <add path="*.aspx" verb="*" type="System.Web.UI.PageHandlerFactory" validate="True"/> <add path="*.ashx" verb="*" type="System.Web.UI.SimpleHandlerFactory" validate="True"/> <add path="*.asmx" verb="*" type="System.Web.Services.Protocols.WebServiceHandlerFactory, System.Web.Services, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" validate="False"/> <add path="*.rem" verb="*" type="System.Runtime.Remoting.Channels.Http.HttpRemotingHandlerFactory, System.Runtime.Remoting, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" validate="False"/> <add path="*.soap" verb="*" type="System.Runtime.Remoting.Channels.Http.HttpRemotingHandlerFactory, System.Runtime.Remoting, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" validate="False"/> <add path="*.asax" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.ascx" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.master" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.skin" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.browser" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.sitemap" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.dll.config" verb="GET,HEAD" type="System.Web.StaticFileHandler" validate="True"/> <add path="*.exe.config" verb="GET,HEAD" type="System.Web.StaticFileHandler" validate="True"/> <add path="*.config" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.cs" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.csproj" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.vb" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.vbproj" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.webinfo" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.licx" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.resx" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.resources" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.mdb" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.vjsproj" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.java" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.jsl" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.ldb" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.ad" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.dd" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.ldd" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.sd" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.cd" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.adprototype" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.lddprototype" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.sdm" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.sdmDocument" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.mdf" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.ldf" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.exclude" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*.refresh" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/> <add path="*" verb="GET,HEAD,POST" type="System.Web.DefaultHttpHandler" validate="True"/> <add path="*" verb="*" type="System.Web.HttpMethodNotAllowedHandler" validate="True"/> </httpHandlers>
From the configuration we can observer the following things
- All the entries include a file name and/or extension to which the handler applies. That is there in the path attribute.
- Verb is a list of verbs to which the handler applies. These verbs corresponding to HTTP request. The verb could be GET, POST or *. The * means anything.
- Here type is the .net assembly which will handle the request for that resource (file name or extension)
To build a custom HTTP handler we need to build a new assembly, and that assembly has to be configured in web.config file as shown in the above. Instead of that we can implement the HTTP handler using “Generic handler”. These Generic Handlers can be call directly with the file name and extension “.ashx” without having to set up web.config or IIS file extension mappings.
In our example we used this Generic handler to create the RSS feed.
Sample Table structure
Create a sample database as below and some records.

Database Table Structure
Steps to Create a RSS feed in asp.net
Note: In the below code we can assume that PostAdapter is responsible for connecting database and getting results. I am not providing the implementation of that PostAdapter. Reader can implement their own implementation.
- Create a new Website

- Create Default.aspx page. This page will show all posts. Add the following code in default.aspx file.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:DataList ID="list1" runat="server"> <HeaderTemplate> <table> </HeaderTemplate> <ItemTemplate> <tr> <td> <p> <a href='ViewArticle.aspx?postid=<%# Eval("PostID") %>'><h1><%# Eval("PostTitle") %></h1></a> author:<%# Eval("PostAuthor") %> Created Date:<%# Eval("PostCreatedDate") %> </p> </td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:DataList> </div> </form> </body> </html> - Add the following code in default.aspx.cs
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { private PostAdapter _adapter; protected void Page_Load(object sender, EventArgs e) { _adapter = new PostAdapter(); list1.DataSource = _adapter.GetPostData(); list1.DataBind(); } } - Create ViewArticle.aspx page. This page will display individual posts. Add the following code in ViewArticle.aspx.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ViewArticle.aspx.cs" Inherits="ViewArticle" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <table> <tr> <td colspan="2"> <asp:Label ID="lblHeader" runat="server" Font-Size="Large"> </asp:Label> </td> </tr> <tr> <td> <asp:Label ID="lblAuthor" runat="server" Font-Size='Smaller'></asp:Label></td> <td> <asp:Label ID="lblDate" runat="server" Font-Size="Smaller"></asp:Label></td> </tr> <tr> <td colspan="2"> <asp:Label ID="lblContnet" runat="server"></asp:Label></td> </tr> </table> </div> </form> </body> </html> - Add the following code in ViewArticle.aspx.cs
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class ViewArticle : System.Web.UI.Page { private PostAdapter _adapter; protected void Page_Load(object sender, EventArgs e) { _adapter=new PostAdapter(); if (Request.QueryString["postid"] != null) { Post _object = _adapter.GetPostById(int.Parse(Request.QueryString["postid"])); lblAuthor.Text = _object.PostAuthor; lblContnet.Text = _object.PostContent; lblDate.Text = _object.PostCreatedDate.ToShortDateString(); lblHeader.Text = _object.PostTitle; } } } - Create a Generic Handler with name “rss”

- Add the following code to the rss.ashx
<%@ WebHandler Language="C#" Class="rss" %> using System; using System.Web; using System.Collections.Generic; public class rss : IHttpHandler { private PostAdapter _adapter = new PostAdapter(); public void ProcessRequest (HttpContext context) { context.Response.Clear(); context.Response.ContentType = "text/xml"; System.Xml.XmlTextWriter cdRSS = new System.Xml.XmlTextWriter(context.Response.OutputStream, System.Text.Encoding.UTF8); cdRSS.WriteStartDocument(); cdRSS.WriteStartElement("rss"); cdRSS.WriteAttributeString("version", "2.0"); cdRSS.WriteStartElement("channel"); cdRSS.WriteElementString("title", "itdiligent.com Latest Articles"); cdRSS.WriteElementString("link", "http://www.mybudgetplans.com"); cdRSS.WriteElementString("description", "Latest articles hosted on itdiligent.com"); cdRSS.WriteElementString("copyright", "Copyright 2009 - 2010 itdiligent.com. All rights reserved."); IList<Post> _postList = _adapter.GetPostData(); foreach (Post _item in _postList) { cdRSS.WriteStartElement("item"); cdRSS.WriteElementString("title", _item.PostTitle); cdRSS.WriteElementString("description", _item.PostContent); cdRSS.WriteElementString("link", "http://" + context.Request.Url.Host + "/ViewArticle.aspx?postid=" + _item.PostID); cdRSS.WriteElementString("pubDate", _item.PostCreatedDate.ToString()); cdRSS.WriteEndElement(); } cdRSS.WriteEndElement(); cdRSS.WriteEndElement(); cdRSS.WriteEndDocument(); cdRSS.Flush(); cdRSS.Close(); context.Response.End(); } public bool IsReusable { get { return false; } } } - Run the application access the rss.ashx
Download the code here. rssfeed

This is awesome….
SleepBarn