An HTTP module is an assembly that is invoked each time a request is made against an application by implementing the IHttpModule interface and handling the event. HTTP modules are invoked as part of the ASP.net request pipeline to access life cycle events throughout the request process. Therefore, the HTTP module gives us the opportunity to check incoming and outgoing requests and take action based on that request. Even we can participate in the management of ASP.net operations by implementing HTTP modules, for example, ASP.net provides sessionstatemodule to provide session-state services to applications.
Well, before using the HTTP module, let's review the lifecycle event for one request:
Here, specifically, HTTP modules are different from HTTP handlers. HTTP modules can be invoked for all requests and responses, and HTTP handlers run only in response to a specific request.
Typical uses of HTTP modules include:
L Security. Because you can check incoming requests, HTTP modules can perform custom authentication or other security checks before requesting a page, an XML Web services, or calling a handler.
L Statistical information and log records. Because HTTP modules are invoked on each request, you can collect request statistics and logging information into a single set of modules instead of being collected in each page.
L A custom header or footer. Because you can modify the outbound response, you can inject content, such as custom header information, into every page or XML Web services response.
The ASP.net HTTP module runs against all requests, similar to the ISAPI filter. However, they are written in managed code and can be fully integrated with the lifecycle of the ASP.net application. The general process for writing asp.net HttpModule is:
L Implement IHttpModule interface.
L HANDLE the Init method and register for the required events.
L HANDLE the event.
L You can also implement the Dispose method if you need to clean it.
L REGISTER the module in Web.config.