Implement the Generic Handler

ASP.NET uses HTTP Handlers to process any HTTP requests made for managed web pages. You can customize the default behavior by implementing custom handlers for custom file types. This is a process rather simple; you should do three things to accomplish it, then some debugging. All will be specified here:

  1. Write the code that handles the requests
  2. Configure IIS to pass the requests to ASP.NET
  3. Configure ASP.NET to process the requests by specifying file extensions

The classic example of creating a generic handler is to build an image handler. We will do the same, too. First add a new Generic Handler to your web site. This will create an .ashx file in the solution. By default, the code will be generated in the same file. This is not desired for us, because when you’ve ended, and try to run your app, it won’t work.

So to work around this, add a CodeBehind attribute to the @WebHandler directive on the top of the page, and specify a class name you like (you should use a syntax like WebHandlerName.ashx.cs). Now cut all the pregenerated code, create the file you specified in the CodeBehind attribute into the App_Code folder, and paste your code there. We should examine this code.

Your class implements IHttpHandler (or IHttpAsyncHandler for asynchronous processing). IHttpHandler has a Boolean IsReusable property. Return true if you’d like to allow your handler to be placed in a pool and reuse it, return false if you’d like to create a new instance of it for every request. IHttpHandler has a void named ProcessRequest. In this void, you need to include the handler logic. In our example, we will use the following:

public bool ProcessRequest(HttpContext context)
{
    context.Response.ContentType = “image/jpeg”;
    context.Response.TransmitFile(@”C:\Users\Username\Pictures\Picture.jpg”);
}

Of course you can specify any path for your image. This handler will always load the Picture.jpg file if being requested. It is important to set a valid MIME type in the ContentType property of the response. We’ve finished our Generic Handler class, so let’s move on.

Now we should set up IIS. To do so, open IIS, select the Default Web Site, then seek for Handler Mappings, there click to Add Managed Handler. You will be able to select our new handler (under the name you gave it) if the code file is in the App_Code directory. Select it, and specify a request path (in our example, *.jpg). You can also give it a friendly name in the Name textbox.

Alternatively, you can set the handler in your web.config file. Seek for the httpHandlers section of system.web. Then use the following syntax:

<add verb=”*” path=”*.jpg” type=”NameOfYourHandlerClass” />

That’s it, we are done. Now quickly drag an image control to a form, set its ImageUrl to a .jpg file. It’s not need to exist. Then when you run your page, you will always get the image specified in the Response.TransmitFile property instead of the image you specified in the ImageUrl.

Further Readings:

HTTP Handlers and HTTP Modules Overview

How to: Register HTTP Handlers

Walkthrough: Creating a Synchronous HTTP Handler

Training Kit: 721-725

Tags: , ,

Leave a comment