Arne Brachhold

Custom HTTP handlers in ASP.Net (.Net 1.1)

Filed under: .Net — arne on May 25, 2005

Custom Webhandlers are an excellent method to create ASP.Net pages which don’t need all the Page and WebControl stuff. Maybe you already created a .aspx file which should only return XML or an image.

If you develop with Visual Studio, it creates automatically an .aspx and .cs file for you and you have to delete all content except the Page Directive from the .aspx file or call Response.End() at Page_Load. This is not very cool and results in useless work.

With HTTP Handlers you can create a handler which doesn’t include the WebControl technology and all the stuff which comes with it.

Step One: Create your HTTP handler class

To create a class which can handle the web-request, you have to implement IHttpHandler from the System.Web namespace. IHttpHandler defines the following elements:

  • public void ProcessRequest(HttpContext context)
    This method will be called to process the request.
  • public bool IsReusable
    This is a read only property which defines whether the same instance of our class may be reused for other requests. This might result in a slight performance improvement.

The following sample code shows the implementation of IHttpHandler:

  1. using System;
  2. using System.Web;
  4. namespace SampleHandler {
  6.     public class MyHandler : IHttpHandler {
  8.         public void ProcessRequest(HttpContext context) {
  9.             context.Response.Write(context.Request.RawUrl);
  10.         }
  12.         public bool IsReusable {
  13.             get { return true; }
  14.         }
  15.     }
  16. }

The example returns just the URL of the requested page. You could also use the XmlTextWriter to return an XML Document or context.Response.WriteFile() to return an image.

Step Two: Register your WebHandler

Once you created your WebHandler, you have to make it available for request. To do that, open your web.config and copy the following lines:

  1. <system.web>
  2.   <httphandlers>
  3.     <add verb="GET" path="smpl.ashx" type="SampleHandler.MyHandler, SampleHandler" />
  4.   <httphandlers>
  5. </system.web>

Verb defines the HTTP verbs for this handler like GET, POST, PUT, etc. If you want to use your handler for all verbs, simply use the wildcard sign (*).

Path defines the path or file which will trigger your WebHandler. You can use the predefined extension for WebHandlers (.ashx) or define your own.
If you do so, you have to register your file extension to ASP.Net in the IIS. This is done in IIS/Website Properties/Home Directory/Application Configuration. Click on add and map the extension to the aspnet_isapi.dll which resides in your .Net Framework directory.

Type is the full qualified class name and its assembly name, separated by comma.

If you don’t have access to the web.config file our your webhoster doesn’t allow you to add custom handlers, simply create the .ashx file and place the following line in it:

  1. <%@ WebHandler Language="C#" Class="SampleHandler.MyHandler" %>

Now point your browser to the .ashx file or the path you specified in web.config and see your own WebHandler in action.