I used VB.NET for the sample code, but you can use the .NET language of your choice. First, launch VS.NET, create an empty project (MyHttpHandler in the sample code), and add a new class to it. Add a reference to the System.Web namespace and add an Imports System.Web statement at the top of the class file. Add an Implements statement to add the IHttpHandler interface. The following code shows the class with an empty interface implementation.
Imports System.Web
Public Class ChartHandler
Implements IHttpHandler
Public ReadOnly Property IsReusable() As Boolean
Implements System.Web.IHttpHandler.IsReusable
Get
End Get
End Property
Public Sub ProcessRequest( _
ByVal context As System.Web.HttpContext) _
Implements _
System.Web.IHttpHandler.ProcessRequest
End Sub
End Class
This example simply outputs some text in response to a request. Add the following code to implement the
ProcessRequest method :
Public Sub ProcessRequest( _
ByVal context As System.Web.HttpContext) _
Implements System.Web.IHttpHandler.ProcessRequest
Context.Response.Write _
("<html><body><h3>Output of Chart " & _
"Handler</h3></body></html>")
End Sub
Note how you gain access to the Response object via the HttpContext parameter.
Next, implement the
IsReusable property as follows:
Public ReadOnly Property IsReusable() _
As Boolean _
Implements IHttpHandler.IsReusable
Get
Return True
End Get
End Property
The property simply returns
True. Compile the project. Next, you must configure it.
Configuring the HttpHandler in IIS
After you develop an HttpHandler or HttpModule you must configure IIS and ASP.NET for the new code to take effect. There are actually two steps involved in running a custom HttpHandler. First, you use the IIS Application Configuration dialog to map the file extension to the ASP.NET engine. Second, you modify the configuration sections in the application's
web.config file to specify the namespace and class you want to use to handle that extension.
For IIS, you add the required file extension in the Application Configuration dialog as shown in
Figure 1. You can add or remove handlers using this dialog.
 | |
| Figure 1: IIS Application Configuration dialog. |
The dialog associates a specific file extension with a specific handlerin this case, tell IIS to use the ASP.NET engine (
aspnet_isapi.dll).
Figure 2 shows how to fill in the dialog for the example. Set the Executable field to the location of the
aspnet_isapi.dll file on your server (your path may vary from the path shown in the figure). Fill in the Extension field with
.chart .
 | |
| Figure 2 : Add/Edit dialog for File Extensions |
Configuring the HttpHandler in Web.config
Next, to configure ASP.NET to recognize new handlers and modules, you modify two configuration sections in the
web.config file for your application: the
<httpHandlers> section, and the
<httpModules> section.
To test the sample HttpHandler you first need to create a new Web project with VS.NET. Next, open the
web.config file of the new application and locate the
<httpHandlers> section. ASP.NET uses the
<httpHandler> section to specify mappings between file extensions and the appropriate HttpHandler. The section will already contain some handlers, for example, the following fragment shows the
<httpHandlers> section on my machine :
<httpHandlers>
<add verb="*" path="*.chart"
type="MyHttpHandler.ChartHandler,MyHttpHandler" />
<add verb="*" path="*.vb"
type="System.Web.HttpNotFoundHandler,System.Web" />
<add verb="*" path="*.cs"
type="System.Web.HttpNotFoundHandler,System.Web" />
<add verb="*" path="*.vbproj"
type="System.Web.HttpNotFoundHandler,System.Web" />
<add verb="*" path="*.csproj"
type="System.Web.HttpNotFoundHandler,System.Web" />
<add verb="*" path="*.webinfo"
type="System.Web.HttpNotFoundHandler,System.Web" />
</httpHandlers>
Each
<add> element in the section contains the following attributes:
verb: specifies the request method GET, POST or both (*)
path: specifies the path of an individual file or set of files (*.chart in our case)
type: specifies the type of the class that will handle the request. For the sample code, the type attribute is
MyHttpHandler.ChartHandler followed by the assembly name
MyHttpHandler.
Save your changes. Next, add a new file with the extension
.chart to the Web application. Navigate to this file from your Web browser, and you will see the text in
Figure 3.
 | |
| Figure 3 : Sample output from the ChartHandler HttpHandler |