When a request is received by the web server or IIS , the request is sent to the worker process.Worker process initializes ASP.NET runtime which handles the request.Request flows through a well defined pipeline to generate the response.
Following are the main components of the HTTP Request pipeline
- HttpModules
- HttpHandler
HttpModule is implemented as a class which has access to both the request and response.It handles the request lifecycle events and can modify the request or the response information.There can be multiple HttpModules for a request.
The events that Modules handle can also be handled in the global application class.For understanding the use of global application class ,please refer global application class.
Using Module to handle the request events has the following advantages.
- Module can be created once and used across different applications.
- Modules allow to keep the even handling logic separate from the application class.This results in better segregation of code.
HttpModule implements the interface IHttpModule.This interface provides the following methods
- Init(HttpApplication) This method is used to register the event handlers.
- Dispose() Disposes the resources which are used by the module.
Following are the steps to implement a custom HttpHandler
- Implement a class which implements the IHttpModule interface.
- Implement the Init method in the class.This method should subscribe to events we need For example, we can subscribe EndRequest event.
- Write the custom logic for the event handler we subscribed to in step 2.
- Finally we register the module in the Web.config file.
public class ICustomModule : IHttpModule { public void Dispose() { } public void Init(HttpApplication context) { context.EndRequest += new EventHandler(context_EndRequest); } void context_EndRequest(object sender, EventArgs e) { //modify the response } }
HttpHandler is a class which ultimately receives the request and generates the response.There can be only one HttpHandler for a request.There are handlers which are provided by the framework such as the page handler which generates the response for .aspx pages. HttpHandler implements the IHttpHandler interface.This interface provides the following members
ProcessRequest Method which generates the response for the request
IsReusable Property which specifies if the handler can be reused.
Some of the inbuilt handlers provided by ASP.NET are
ASP.NET page handler HTTP handler for ASP.NET (*.aspx) pages .
Web service handler HTTP handler for web services (*.asmx)
Following are the steps to implement a custom HttpHandler
- Implement a class which implements the IHttpHandler interface.
- Implement the ProcessRequest method in the class.This method should generate the response.
- Implement IsReusable boolean property to specify if the HttpHandler can be reused.
- Finally we register the Handler in the Web.config file.
public class ICustomHandler :IHttpHandler { public bool IsReusable { get { return false; } } public void ProcessRequest(HttpContext context) { } }
In web.config
<httpHandlers> <add verb="*" path="*.cst" type="ICustomHandler"/> </httpHandlers>
Leave a Reply