What is an HttpHandler?
It is the earliest point where you have access to a request that is made to IIS. In a typical application, a user calls .aspx pages, which maps to an HttpHandler, that handler is the PageHandlerFactory. An HttpHandler is just like an aspx page except it doesn't have all of the UI elements therefore it doesn't have to process them. HttpHandlers are therefore more reusable.
Why would you want to use an HttpHandler?
Some great examples of why you would want to use an HttpHandler is in the case when you want to return an image from the database. You could create an HttpHandler to call the database and return the Image. Typically I used to create a page for this and override the response type to return the image. With an HttpHandler, you can skip all the extra page processing and just return the image.
Another great example is when you want to download a file, such as a an excel file. You could pass in a datatable, convert it, and then return it as an excel file. Again, I used to create a page for this and override the response content types to do this, but an HttpHandler is a far better solution to the problem.
Real-World Examples
A real world example is on the blog engine I am using to write this post, SubText. It uses a custom HttpHandler to map URL's to controls.
Another GREAT example is the Microsoft AJAX framework. If you look at the base web.config file they give you. See below for an extract of the <httpHandlers> section :
34 <httpHandlers>
35 <remove verb="*" path="*.asmx"/>
36 <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
37 <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
38 <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
39 </httpHandlers>
As you can see, a few custom file extensions are mapped to HttpHandlers. This is how the Microsoft AJAX framework does most of the heavy lifting for you. The HttpHandlers that are included handle inserting the necessary scripts into the page to perform the AJAX requests.
Demo
I made up a demo solution to show the basics of creating a HttpHandler. Whats in the demo is 3 examples. The first example is just a basic HttpHandler. What it does when called is just return some basic text to the browser. The second example shows creating a simple datatable and exporting it as tab delimited excel file. The third example shows a more useful example where we call a database and export the results as a tab delimited excel file.
The real key to the HttpHandlers, besides implementing the interface, is adding the directives in the web.config file.
1 <?xml version="1.0"?>
2 <configuration>
3 <appSettings />
4 <connectionStrings/>
5 <system.web>
6 <compilation debug="true" />
7 <authentication mode="Windows" />
8
9 <!-- add this under the <system.web> -->
10 <httpHandlers>
11 <!-- you could specify a wildcard with a extension, or a specific name of a "page" to call -->
12 <!-- you can also create an extension of your choosing, and tell IIS to process your extension as a handler -->
13 <!-- <add verb="*" path="*.ashx" type="BasicHelloWorldHandler"/> -->
14 <add verb="*" path="BasicExport.ashx" type="ExportToExcelHandler"/>
15 <add verb="*" path="ExportDB.ashx" type="ExportToExcelDBHandler"/>
16 </httpHandlers>
17 </system.web>
18 </configuration>
The above web.config file has commented out code which shows that we can map any file with the extension .ashx . The other two <add> elements in the <httpHandlers> section shows how we can map a specific filename to the HttpHandlers we wrote. As you can see, you pass in to parts, the "Path" which is the filename to map, and the "Type" which is the class to map the call to the filename to.
Download Files
You can download the demo solution
here.
Its a website project, so create a directory for it, and extract the files there. Then create a virtual directory for it in IIS or just run it in VisualStudio2005.
264c2160-15e4-4167-b700-72e7967d19d4|4|2.3