HTTP Handlers And HTTP Modules In ASP.NET

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,
  1. Public class CSCornerCustomHandler: IHttpHandler {  
  2.     public void ProcessRequest(HttpContext context) {  
  3.         // write your custom logic here, you can use context object to access request,reponse object     
  4.     }  
  5.     Public bool IsReusable {  
  6.         get {  
  7.             return false;  
  8.         }  
  9.     }  
  10. }  
Register your custom HTTPHandler in web.config
  1. <HttpHandler>     
  2.    <add verb="*" , path= "*.cscorner", type=""/>    
  3. <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.
  1. BeginRequest()
  2. EndRequest()
  3. 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,
  1. Public class CSCornerModule: IHttpModule {  
  2.     public void Init(HttpApplication context) {  
  3.         context.BeginRequest += new EventHandler(this.context_BeginRequest);  
  4.         context.EndRequest += new EventHandler(this.context_EndRequest);  
  5.         context.AuthenticateRequest += new EventHandler(this.context_AuthenticateRequest);  
  6.     }  
  7.     public void context_AuthenticateRequest(object sender, EventArgs e) {  
  8.         //write your custom logic here to authenticate any request coming to server before it is processed by server.    
  9.         //This is the best place to validate all the requests before it gets processed by server     
  10.     }  
  11.     //implement other events here as per the application requirement    
  12.     Public void Dispose() {  
  13.         //logic to dispose the objects goes here     
  14.     }  
  15. }  
Register HttpModule
  1. <HttpModules>  
  2.    <add name="somename" type="customHttpModule"/>  
  3. </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.


Similar Articles