Handling File Types of Your Own
To write an HttpHandler, you create a class that implements the IHttpHandler interface. All of the handlers in Listing 8.1 do this. You might recall from Chapter 2, "Classes: The Code Behind the Objects," that an interface is used to ensure that a well-known means of communicating with some other code is available. For ASP.NET to communicate with an HttpHandler, it must have a couple members defined by the interface. Listing 8.2 shows the basic interface.
Listing 8.2. A class implementing IHttpHandler
C#
VB.NET
public class MyHttpHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
// do something here
}
public bool IsReusable
{
get { return true; }
}
}
The ProcessRequest() method is where we do work in response to the request. ASP.NET passes in a reference to the HttpContext object associated with the request. You'll notice in Visual Studio that as soon as you type a period following the context parameter, Intellisense will show you properties that correspond to all the familiar objects you might use in a page (see Figure 8.1).
Public Class MyHttpHandler
Implements IHttpHandler
Public Sub ProcessRequest(context As HttpContext)
' do something here
End Sub
Public ReadOnly Property IsReusable() As Boolean
Get
Return True
End Get
End Property
End Class
Figure 8.1. The properties of the HttpContext object in an HttpHandler.

Listing 8.3. Adding an HttpHandler to your web.config
The element that does the actual work is the <add /> element. The verb attribute indicates the types of requests that are covered (GET or POST), and you can use an asterisk as a wildcard. The path attribute describes the file path of the request. In this example we're using a wildcard with ".jpg" to indicate that we want to handle all requests for .jpg files, but you could just as easily specify anything else, such as "mypage.aspx." The <add /> elements are considered in the order that they appear, so a request that fits the path of two elements will be serviced by the class in the last entry. Finally, the type attribute indicates the name of the class implementing IHttpHandler and the assembly name in which it resides, looking in the /bin folder. Keep in mind that the class name should be fully qualified, meaning that the namespace should be included.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="*.jpg" type="MyClass, MyDll" />
</httpHandlers>
</system.web>
</configuration>