This 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:

image


Select "Configuration…"

image


Select "Add"

image


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);
   
} </p>

    </font>public bool IsReusable {
       
get {
           
return true;
       
}
   
}
} </div> </blockquote>

 
Now, I have drastically oversimplified the process (as I pointed out above, there are much better tutorials elsewhere online — this post is only meant to give you the information necessary to at least be able to answer the question put forth above), but that is basically it.
 
Now that we’ve covered HttpHandlers, where do .ASHX files fit in to all of this?  Well, let’s say that you don’t want to go through all the trouble (or can’t due to your hosting provider) of setting up a custom extension in IIS and configuring them through the web.config file, but you still want to take advantage of HttpHandlers.  It turns out that ASP.NET has a build in extension for just such an occasion — .ASHX.
 
To use the same RSS example, the exact same code above could be implemented above in a .ASHX file.  The only difference would be that instead of encapsulating the code within a .cs file, you would instead create a .ashx file, and instead of the feed being fetched through MyFeed.rss, it would be fetched through MyFeed.ashx.  In this case, I’m not sure that RSS feeds are the best example for these types of files, but I have worked on several projects that have taken advantage of .ashx files to serve up images dynamically from a database.  If you’re interested, there is a great tutorial that covers that topic here.
 
That should give you a pretty good idea of how HttpHandlers and .ASHX files work.  If you have any further questions, comments are always welcome, and if you’re willing to look, there is a wealth of information on this subject available online.