ASP.NET

IHttpHandler

Here it is. Shield your eyes while you look at Listing 18-2.

Listing 18-2

   public interface IHttpHandler
  {
    void ProcessRequest(HttpContext ctx);
    bool IsReusable {get;}
  }

There's really not much to it, is there? The interface includes a method named ProcessRequest and a property named IsReusable. If the handler instance can be used multiple times, then IsReusable should return true. The heart of the handler is the ProcessRequest method that includes a single parameter: the current HttpContext.

Once a request finally arrives at the handler (through the ProcessRequest method), ProcessRequest can literally do anything to respond to the request. The Trace.axd handler responds to a GET request by listing the requests being tracked by the runtime. The forbidden handler responds by throwing up a roadblock so the client can't see the source. A custom Web service might respond to the request by parsing the XML payload, constructing a call stack, and making a call to an internal method.

The next page example illustrates how to handle forms processing in a custom handler.

Implementing IHttpHandler

Implementing IHttpHandler is simple-at least from the architectural point of view. The ProcessRequest method takes a single parameter-the current HttpContext. However, the code inside ProcessRequest is free to do just about anything possibly making the internal processing quite complex! The following example illustrates taking over the entire form-rendering process to display a list of choices within a combo box, allowing the end client to select from the choices, and finally rendering the chosen item.