To create an HttpHandler, we need to implement the
IHttpHandler interface. The IHttpHandler contains one property and one method.
Listing 1 - IHttpHandler interface
public interface IHttpHandler
{
bool IsReusable { get; }
void ProcessRequest(HttpContext context);
}
The property IsReusable specifies
whether the ASP.NET should reuse the same instance of the HttpHandler for
multiple requests.
The ProcessRequest () method is
where you actually implement the logic to handle the request.
After you create the HttpHandler, you have to map a path to
the handler in the web.config.
A typical web.config configuration for the handler looks
like the following.
Listing 2 - web.config setting for httpHandler
<configuration>
<system.web>
...
<httpHandlers>
<add verb="*" path="getAvatarImage.img" type="Sample.ImageHandler, Sample" />
</httpHandlers>
The verb part is just what you want it to respond to (GET,
POST, etc.). Path is the file or file extension you want it to respond to and
Type is - ClassName, Assembly. For the above web.config setting, each request
to the getAvatarImage.img will be sent to the handler and the ProcessRequest will
be executed. When we use a new file extension like "getAvatarImage.img"
or "getAvatarImage.do," we need to create IIS
extensions mapping for the new extensions.
We will be using the built-in extensions (.ashx) that are
already mapped to ASP.NET so that we can avoid the necessity to modify the IIS
extensions mapping. With .ashx extension, there is no requirement for
registration in the web/machine.config.