HTTPHandlers and HTTP Modules
HTTP Handlers and HTTP Modules are associated with all the client requests. One request can have multiple HTTP Modules associated with it but can be handled by only one HTTP Handler. The type of HTTPHandler is decided by the extention of the request file like .aspx, .asmx. ASP.Net has it's own HTTPHandlers and HTTPModules that are used with client requests. We can create our own custom HTTPHandlers and HTTPModules based on the application requirements, which will be explained below,
HTTPHandlers
ASP.Net HTTPHandler is a process that runs in response to a request made to an ASP.Net application. When a client makes a request to a server resource, each request is handled by HTTPHandler based on the request file extension (.aspx, .asmx etc). The most common Handler is the ASP.Net page handler that processes .aspx files. When users request a .aspx file, the request is processed by the page through the page handler.
By default ASP.Net handlers have .ashx file extension.
ASP.Net has number of built in HTTPHandlers which handle different files like .aspx, .asmx based on extension of file. In spite of these built in HTTPHandlers you can always create your own custom handlers to handle some specific extension of file.
Custom HTTPHandlers
You can create your own HTTPHandlers and register it for the request handling in web.config using <HttpHandlers/> element. To create a custom HTTPHandler you have to create a class and need to implement IHttpHandler interface. IHttpHandler has 2 abstract methods and 1 property that can be defined in your custom class.
Method - ProcessRequest()
Property - ISReusable
ProcessRequest(HttpContext context)
This is a method which is called once the request is received. This method gets the HttpContext object as a parameter and can use the same for writing custom logic. Using the HttpContext object you can access request and response object to write the custom processing logic.
IsReusable
This is a property that if it is set to true, the same object will be used for processing further requests of the same type. If it is set to false the handler object is disposed after request.
Please see the sample code below,
- Public class CSCornerCustomHandler: IHttpHandler {
- public void ProcessRequest(HttpContext context) {
-
- }
- Public bool IsReusable {
- get {
- return false;
- }
- }
- }
Register your custom HTTPHandler in web.config
- <HttpHandler>
- <add verb="*" , path= "*.cscorner", type=""/>
- <HttpHandler>
Now any request that comes for .cscorner extension would be handled by your custom HTTPHandler.
HTTPModules
Unlike HTTPHandlers, HTTPModules are executed with all the client requests coming in irrespective of the extension of the requested file.In single request processing. There can be more than one HTTPMudules which gets executed. HTTPModules handles application events.
- BeginRequest()
- EndRequest()
- AuthenticateRequest()
You can create you own custom HTTMModules using IHttpModule interface.This interface contains 2 methods which can be implemented in your custom Module class. These methods are,
Init()
Takes Http Application object as a parameter which allows HttpModule to register events.
Dispose()
To dispose the Module object before garbage collector.
For example see the below sample code,
- Public class CSCornerModule: IHttpModule {
- public void Init(HttpApplication context) {
- context.BeginRequest += new EventHandler(this.context_BeginRequest);
- context.EndRequest += new EventHandler(this.context_EndRequest);
- context.AuthenticateRequest += new EventHandler(this.context_AuthenticateRequest);
- }
- public void context_AuthenticateRequest(object sender, EventArgs e) {
-
-
- }
-
- Public void Dispose() {
-
- }
- }
Register HttpModule
- <HttpModules>
- <add name="somename" type="customHttpModule"/>
- </HttpModules>
Summary
HTTM Handlers and HTTP Modules are very important to perform any validations or checks before the rquest is being processed by the server. Generally HttpModules are used for implementing security or logging in the application in specific event.
References
- https://support.microsoft.com/en-us/help/307985/info-asp-net-http-modules-and-http-handlers-overview
- https://www.dotnetcurry.com/aspnet/1264/aspnet-http-handlers-http-modules
Thank you for reading and please suggest if I missed something important to add in this blog as this is my very first blog.