ASHX files and HttpHandlers
Posted by: Justin in Programming, tags: ASP.NET, InterviewingThis is the second entry in my the series of entries that I’ve devoted to answering Scott Hanselman’s ASP.NET interview questions. The first entry covered the events fired as part of the ASP.NET Page lifecycle.
Alright, so here is the question: What are ASHX files? What are HttpHandlers? Where can they be configured?
And, here is the answer (to the best of my abilities): Let’s start with HttpHandlers, because that will take us into ASHX files. In the simplest (programmatic) terms, an HttpHandler is any class that implements the System.Web.IHttpHandler interface. OK, you might be saying, so where are they used? Well, everywhere. Any time a request is made to the ASP.NET worker process, the request is passed on to the HttpHandler that has been configured to handle such requests. For example, when you go to http://localhost/default.aspx, IIS sees this request as one that should be handled by ASP.NET (more on how to configure this later) and hands it off to the worker process. Then, by looking at the extension, ASP.NET knows to process the request using System.Web.UI.PageHandlerFactory. Similarly, if a request is made to retrieve the web.config file, ASP.NET knows to process the page using System.Web.HttpForbiddenHandler. In fact, if you open up the machine.config file, you should see several recognizable page extensions and the HttpHandlers that are responsible for each:
<add verb="*" path="*.aspx" type="System.Web.UI.PageHandlerFactory" />
<add verb="*" path="*.config" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.asmx" type="System.Web.Services.Protocols.WebServiceHandlerFactory" />
These recognizable extensions are provided by default in ASP.NET. So, you can spend your entire developer career knowing nothing of HttpHandlers and still manage to create functional and successful sites. However, to use a well known example, suppose you want to create an RSS feed to syndicate content on your site. Sure, there are plenty of tutorials online that will help you create the XML and save it to the output stream on an ASPX page, but you want to be fancy and make it so that all of your RSS feeds end in a .rss extension. What could you do? Well, if you haven’t guess, HttpHandlers would be your answer. Remember above where I said that I’d eventually show you how to instruct IIS to pass certain requests to the ASP.NET worker process? It turns out that now would be the perfect time (if you’d like a more in-depth tutorial on how to syndicate your content using HttpHandlers, there is a great article here).
For this example, let’s assume your feed is to be posted at MyFeed.rss. So, when this request is made to IIS, you want to make sure that the .rss extension is sent to the ASP.NET worker process. Here is how:
Open up IIS:
Select "Configuration…"
Select "Add"
Use .rss as the extension and aspnet_isapi.dll as the executable (you can copy this from another extension). I unchecked "Check that the file exists" so that the file doesn’t actually need to exist when the request is made. Once you have this configured, IIS will now know to send all requests with a .rss extension to the ASP.NET worker process. However, the ASP.NET process still does not know how to process the request. This is solved by adding a few lines to the system.web section of the web.config file (similar to how *.aspx requests are defined in the machine.config file example from above):
<system.web>
<httpHandlers>
<add verb="*" path="*.rss" type="type,assemblyname" />
</httpHandlers>
</system.web>
Where type is the actual type and assemblyname is the actual assembly name. To build the assembly, you would create a new class (inside of a .cs file) that implements the IHttpHandler interface and defines the ProcessRequest method and the IsReusable property. For example:
public class YourRssHandler : IHttpHandler {
public void ProcessRequest(HttpContext context) {
//You would build the actual rss feed below
string rssXML = "<rss>Dummy rss xml feed</rss>";
context.Response.ContentType = "text/xml";
context.Response.Write(rssXML);
}public bool IsReusable {
get {
return true;
}
}
}


Entries (RSS)