HttpHandlers and HttpModules in ASP.NET

By | March 3, 2014

In order to understand the concept of HttpHandlers and HttpModules in ASP.NET, we must need to understand first, how our web server and ASP.NET collectively reacts when a user requests for a particular resource? Our web server i.e. IIS does the following:

  • It looks for the extension of the requested resource file e.g. (.aspx, .ascx etc.).
  • It looks for ISAPI extension mapped against this file extension.
  • If found a mapping, then pass request to that particular ISAPI extension.

So, For example, in case of a resource Page1.aspx file, web server will forward that request to ASP.NET Engine through ISAPI extension (aspnet_isapi.dll). At high level, ASP.NET Engine is a kind of a HTTP Pipeline because HTTP request moves through a number of HttpModules and finally reaches a particular HTTP Handler as shown in following figure.

ASP.NET Engine

Now, by default, request for an ASP.NET page is handled by a HttpHandler called “ASP.NET Page Handler”. Also, this request passes through a number of build-in HttpModules like OutputCache, Session, WindowsAuthentication, FormsAuthentication, RoleManager etc.

Now, we can define both of these concepts as:
HTTPHandler:
ASP.NET Engine uses HttpHandlers to handle specific requests on the basis of their extensions. ASP.NET Page Handler handles all requests coming for (.aspx) pages. We can define our own custom HttpHandler to handle a specific request with a specific extension, say .jpeg, .gif, or .imran. But there will always be only one handler for a specific request.
HTTPModules:
ASP.NET Engine uses HttpModules to inject some specific functionality alongwith ASP.NET default functionality for all incoming requests regardless of its extensions. There are a number of built-in modules already available in ASP.NET HTTP Pipeline as we discussed earlier. But we can write our own custom HTTP module to perform some additional functionality (for example, URL rewriting or implementing some security mechanism) for all incoming requests.

Now, hopefully it will be clear that how request flows as well as role of HttpHandlers and HttpModules in ASP.NET request flow.

Other Related Articles:

 

Top 10 Interview Questions and Answers Series: