Mega Code Archive

 
Categories / ASP.Net Tutorial / Development
 

RSS Handler

File: App_Code\RSSHandler.cs using System; using System.Web; using System.Net; using System.IO; namespace MyNamespace {     public class RSSHandler : IHttpAsyncHandler     {         private HttpContext _context;         private WebRequest _request;         public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)         {             _context = context;             _request = WebRequest.Create ("http://msdn.microsoft.com/asp.net/rss.xml");             return _request.BeginGetResponse(cb, extraData);         }         public void EndProcessRequest(IAsyncResult result)         {             string rss = String.Empty;             WebResponse response = _request.EndGetResponse(result);             using (response)             {                StreamReader reader = new StreamReader(response.GetResponseStream());                rss = reader.ReadToEnd();             }             _context.Response.Write(rss);         }         public bool IsReusable         {             get { return true; }         }         public void ProcessRequest(HttpContext context)         {             throw new Exception("The ProcessRequest method is not implemented.");         }     } } Register it in your web configuration file.  File: Web.Config <configuration>     <system.web>       <httpHandlers>         <add path="*.rss" verb="*" type="MyNamespace.RSSHandler"/>       </httpHandlers>     </system.web> </configuration> If you have a news reader, such as SharpReader, then you can enter a path like the following in the reader's address bar: http://localhost:2026/YourApp/news.rss File: ShowRSSHandler.aspx <%@ Page Language="C#" %> <%@ Import Namespace="System.IO" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">     void Page_Load()     {         string pagePath = Request.Url.OriginalString;         string rssPath = Path.ChangeExtension(pagePath, ".rss");         srcRSS.DataFile = rssPath;     } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">     <title>Show RSS Handler</title> </head> <body>     <form id="form1" runat="server">     <div>     <asp:GridView         id="grdRSS"         DataSourceID="srcRSS"         AutoGenerateColumns="false"         Runat="server">         <Columns>         <asp:TemplateField HeaderText="Articles">         <ItemTemplate>             <asp:HyperLink                 id="lnkRSS"                 Text='<%# XPath("title") %>'                 NavigateUrl='<%# XPath("link") %>'                 Runat="server" />         </ItemTemplate>         </asp:TemplateField>         </Columns>     </asp:GridView>     <asp:XmlDataSource         id="srcRSS"         XPath="//item"         Runat="server" />     </div>     </form> </body> </html>