Tuesday, January 20, 2009

HTTP Handler in detail

The process that runs in response to a request made to an ASP.NET Web application is the HTTP handler ( or endpoint). When users request an .aspx, .asmx, or .ashx file, the page processes the request through the page handler.

Uses:

  • It can be created to render custom utput to the browser.
  • It add new functionalities to the Web server.
  • It can be created to generate RSS-formatted XML. You can then bind a file name extension such as .rss to the custom handler. When users send a request to your site in a format that ends in .rss, the Web application sends the request to the HTTP handler to process the request.
  • Image server. HTTP handlers can be used to resize the images in a Web application. When you want a Web application to resize the images, you can write a custom handler to resize the images, and then send them to the user as the handler's response.

ASP.NET maps HTTP requests to HTTP handlers, based on a file name extension. Each HTTP handler can process individual HTTP URLs or groups of URL extensions in an application. ASP.NET includes the following built-in HTTP handlers:
ASP.NET page handler (*.aspx) : default HTTP handler for all ASP.NET pages.
Web service handler (*.asmx): default HTTP handler for Web service pages created as .asmx files in ASP.NET.
Generic Web handler (*.ashx): default HTTP handler for all Web handlers that do not have a user interface (UI) and that include the @WebHandler directive.
Trace handler (trace.axd): displays current page trace information.

A Http Handler for performing synchronous processing of requests:

public class HelloWorldHandler : IHttpHandler
{
public HelloWorldHandler()
{
}

//The ProcessRequest method is responsible for processing individual
//HTTP requests. You can write the code that produces the
//output for the handler by using the ProcessRequest method.
public void ProcessRequest(HttpContext context)
{
HttpRequest Request = context.Request;
HttpResponse Response = context.Response;
// This handler is called whenever a file ending in .sample is
//requested. A file with that extension does not need to exist.
Response.Write(".HTML.");
Response.Write(".body.");
Response.Write(" .h1.Hello from a synchronous custom HTTP handler./h1");
Response.Write("./body.");
Response.Write("./html.");
}

// This property specifies whether the IHttpHandlerFactory object can put the handler in a pool and reuse it. If the handler cannot be pooled, the factory should create a new instance of the handler

public bool IsReusable
{
// To enable pooling, return true here. This keeps the handler in memory.
get { return false; }
}
}


To register the handler :

.configuration.
.system.web.
.httpHandlers.
.add verb="*" path="*.sample" type="HelloWorldHandler"/.
.httpHandlers.
.system.web.
.configuration.

Note : "*" means all of the http verbs, eg. GET,POST

To register the handler in IIS 7.0 running in Integrated mode

.configuration.
.system.webServer.
.handlers.
.add verb="*" path="*.sample" name="HelloWorldHandler" type="HelloWorldHandler".
./handlers.
./system.webServer.
./configuration.

Note : When a HTTP Handler class is created, the handler can respond to any file name extension that is not already mapped in IIS and in ASP.NET. For ASP.NET to know which handler to use for your custom file name extension, you must map the extension in IIS to ASP.NET. Then, in the application, you must map the extension to the custom handler.
By default, ASP.NET maps the file name extension, .ashx, to an HTTP handler. If you create an HTTP handler class that has the file name extension, .ashx, the handler is automatically registered with IIS and ASP.NET. The advantage of not using the .ashx file name extension is that the handler is then reusable for different extension mappings. If you want to create a custom file name extension for a handler, you must explicitly register the extension with IIS and ASP.NET. After you create the custom HTTP handler class, you must register it in the application's web.config file

source of reference : MSDN

No comments:

 
Google