HTTP handler and HTTP Modules

HTTP handler

ASP.NET HTTP handler is the process or class that runs in response to a request made to an ASP.NET Web application. HTTPHandlers are responsible for intercepting requests made to ASP.NET web application server. When users request an .aspx file, the request is processed through the page handler.

The most common handler is an ASP.NET page handler that processes ".aspx" files. Whenever request for .aspx file comes to server the request is processed by the page through this page handler. ASP.NET maps HTTP requests to HTTP handlers based on a file name extension.

ASP.Net has other built in HTTP handlers as well as follows,

  •      • Page Handler (.aspx): handles Web pages
  •      • User Control Handler (.ascx): handles Web user control pages
  •      • Web Service Handler (.asmx): handles Web service pages
  •      • Trace Handler (trace.axd): handles trace functionality

You can create your own custom HTTP handlers that render custom output to the browser. Typical scenarios for custom HTTP Handlers in ASP.NET for example are, Dynamically created images, CustomRSS feeds

Creating a Custom HTTP Handler:- To create a custom HTTP handler, create a class that implements the IHttpHandler interface. Alternatively, you can implement IHttpAsyncHandler to create an asynchronous handler. 



HTTP Modules

HTTP module is an assembly that is called on every request that is made to your application. HTTP modules are called as part of the request pipeline and have access to life-cycle events throughout the request. HTTP modules therefore let you examine incoming requests and take action based on the request. They also let you examine the outgoing response and modify it.

Modules must be registered to receive notifications from the request pipeline. The most common way to register an HTTP module is in the application's Web.config file. When ASP.NET creates an instance of the HttpApplication class that represents your application, instances of any modules that have been registered are created. 

When application events are raised, the appropriate method in your module is called. The method can perform whatever logic is required, such as checking authentication or logging request information. 

Creating an HTTP Module
The general process for writing an HTTP module is as follows:

  •      •Create a class that implements the IHttpModule interface.
  •      •Write a handler for the Init method. 
  •      •The initialization method should initialize your module and subscribe to any application events you need. For example, if you want to append something to responses, you might subscribe to the EndRequest event. 
  •      •If you want to perform custom authentication logic, you might subscribe to the AuthenticateRequest event. 
  •      •Write code for the events that you have subscribed to.
  •      •Optionally implement the Dispose method if the module requires cleanup.
  •      •Register the module in the Web.config file.



HTTP Modules vs Global.asax
You can implement much of the functionality of a module in the application's Global.asax file, which enables you to respond to application events. However, modules have an advantage over the Global.asax file because they are encapsulated and can be created one time and used in many different applications. By adding them to the global assembly cache and registering them in the Machine.config file, you can reuse them across applications. 

The advantage of using the Global.asax file is that you can handle other registered events such as Session_Start and Session_End. In addition, the Global.asax file enables you to instantiate global objects that are available throughout the application.

Comments