Team LiB
Previous Section Next Section

Creating Custom HttpHandlers

An HttpHandler is a class that has a single method that is invoked to handle a request. Rather than attaching itself to application events, an HttpHandler registers itself to listen for requests that match a specific path or wildcard expression. For example, you can create an HttpHandler that will respond to requests for all files ending in the .myHandler extension.

Another really good use for HttpHandlers is to create a handler that deals with .html files. Rather than have a single product.aspx page, you can have a virtual .html page for every product ID in your catalog. This allows a page to be bookmarked (and possibly spidered by search engines) and still be every bit as dynamic as a regular ASP.NET page.

Building a Synchronous HttpHandler

If you still have the HttpModule solution, you can add HttpHandler code to it. Add a new class to the SampleHttpModule project and call it HtmlHandler. Listing 25.3 shows the contents of that class.

Listing 25.3. The HtmlHandler Class
using System;
using System.Web;
using System.Collections;

namespace SampleHttpModule
  /// <summary>
  /// Summary description for HtmlHandler.
  /// </summary>
  public class HtmlHandler : IHttpHandler
    public HtmlHandler()
      // TODO: Add constructor logic here
    #region IHttpHandler Members

    public void ProcessRequest(HttpContext context)
      string prodId = context.Request.FilePath.Substring(
      HttpResponse Response = context.Response;

      Response.Write("The following is the detail for product ID " + prodId + "<Br>");
      Response.Write("This product costs: " +
      string.Format("{0:C}<br>", 49.99));


    public bool IsReusable
      // TODO:  Add HtmlHandler.IsReusable getter implementation
      return false;


The ProcessRequest method takes an instance of the HttpContext class and provides processing for it. In this case, I create a simple HTML document that contains product details. Instead of passing something like "productId=222", I can browse to "prod222.aspx" and it will have the same effect. To rig up this handler, add the following lines to your web.config:

  <add verb="*" path="prod*.aspx"
    type="SampleHttpModule.HtmlHandler, SampleHttpModule" />


If the handler you are creating is not for an extension that is already managed by .NET, you will need to go into the IIS management console and change the configuration of the application's virtual directory. Add a file extension mapping of your custom extension to the .NET ISAPI filter (aspnet_isapi.dll) and your custom HttpHandler should then work properly. To test it out before making a change to IIS, you can always set your path to use an existing extension, such as .aspx.

Rebuild the solution and then browse to http://localhost/custommodule/prod222.aspx. Figure 25.4 shows that not only do we get a custom implementation of an .aspx page, but that the HttpModule we built previously is still active and controlling the format of our currency, even though the currency was displayed inside an HttpHandler.

Figure 25.4. The custom HtmlHandler in action.

    Team LiB
    Previous Section Next Section