Tuesday, February 15, 2011

The HTTP Handler

An HTTP handler is code that executes when an HTTP request for a specific resource is made to the server. For example, when a user requests an .aspx page from IIS, the ASP.NET page handler is executed. When an .asmx file is accessed, the ASP.NET service handler is called.An HTTP handler component is an instance of a class that implements the IHttpHandler interface. This component is a pillar of the ASP.NET runtime architecture. Here’s the defnition of the interface:

//C#
public interface IHttpHandler
{
public void ProcessRequest(HttpContext context) ;
public bool IsReusable;
}

The name of the method ProcessRequest says it all about the intended semantics. It takes the context of the request as the input and ensures that the request is serviced. In the case of synchronous handlers, when ProcessRequest returns, the output is ready for forwarding to the client. (It is not of primary importance here, but HTTP handlers can also work asynchronously according to the methods in the IHttpAsyncHandler interface.)

In Visual Studio, you build an ASP.NET application as a collection of Web Forms pages. Each page consists of two fles: an .aspx markup fle describing the expected HTML template and a C# (or Visual Basic) class file that contains postback handlers and any ancillary methods.

Where’s the HTTP handler, then? Who writes the HTTP handler for each and every ASP.NET request that originates within an application? Is the Web Forms model really centered on the concept of an HTTP handler?

The answer is in the underlying design pattern used to implement the Web Forms model.
Known as Page Controller, the pattern suggests that you arrange the processing of an HTTP request around the concept of the page. Processing the request is a task that goes through a number of steps, such as instantiating the page, initializing the page, restoring the page’s state, updating the page, rendering the page, and unloading the page. In the implementation of the pattern, you start from a base page class and define a strategy to process the request—the page life cycle. In the implementation of the page life cycle,you come up with an interface of virtual methods and events that derived pages will have to override and handle. Derived page classes are known as code-behind classes in ASP.NET jargon.

In ASP.NET, the base page class is System.Web.UI.Page and, guess what, most of what it does is implement the IHttpHandler interface. (See Figure Below Figure)





You can create your own custom HTTP handlers, register them with IIS, and receive notice when a specific request has been made. This allows you to interact with the request and write your own custom output to the browser. Please check Creating Custom HTTP Handlers.


4 comments:

  1. This is a nice article..
    Its easy to understand ..
    And this article is using to learn something about it..

    c#, dot.net, php tutorial, Ms sql server

    Thanks a lot..!
    ri70

    ReplyDelete
  2. Good Article... Appreciated

    ReplyDelete
  3. Nice article. It is really helpful.

    ReplyDelete