jQuery REST RSS Demo in ASP.NET and C#

Old-school example of using jQuery to return an RSS feed from an ashx proxy on the server. This method of using an ashx file that exposed data to the client in some format (nowadays, JSON, but in this example, XML) was probably the simplest way to build basic AJAX functionality in an ASP.NET website, before things like ASP.NET MVC and Microsoft’s WebAPI became standard. This was one of the most popular pages on my old website.

Demo currently unavailable

HTML

<input type="text" id="rssURL" style="width:80%;" />&nbsp;
<input type="button" onclick="loadData()" value="Get RSS" />

JavaScript to retrieve the XML from a handler on the server

$(document).ready(function() {
    $("#rssURL").val("http://feeds.encosia.com/Encosia");
    loadData();
});
        
function loadData()
{           
    $("#feedContainer").empty();
    
    $.get("FeedServer.ashx?url=" + $("#rssURL").val(), function(data) {                         
        
        $(data).find('item').each(function() {
            var $item = $(this);
            var title = $item.find('title').text();
            var link = $item.find('link').text();
            var description = $item.find('description').text();
            var pubDate = $item.find('pubDate').text();

            var html = "<div style=\"margin-bottom:8px;\">";
            html += "<h3>" + title + "</h3>";
            html += "<em>" + pubDate + "</em>";
            html += "<p>" + description + "</p>";
            html += "<a href=\"" + link + "\" target=\"_blank\">Read More</a>";
            html += "</div>";

            $('#feedContainer').append(html);
        });
    });
}                        

The server-side code to expose the RSS feed

<%@ WebHandler Language="C#" Class="FeedServer" %>

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Services;
using System.Xml.Linq;

public class FeedServer : IHttpHandler 
{        
    public void ProcessRequest (HttpContext context) 
    {
        string url = context.Request.QueryString["url"];
        
        XDocument feedXML = XDocument.Load(url);  

        context.Response.ContentType = "text/xml";
        context.Response.Write(feedXML.ToString());
    }        
}