ASP.NET offers two methods for extending standard processing behavior: HTTP modules and HTTP handlers.
An HTTP module is an assembly that is called on every request made to your application. HTTP modules are called as part of the ASP.NET request pipeline and have access to life cycle events throughout the request (source).
There are two steps required to create custom HTTP module:
- Create class which implements [cci]IHTTPModule[/cci] interface
- Register your module in web.config
Each time a request is made to your application, [cci]Init[/cci] method is being called. Having a reference to HTTPApplication you can subscribe to the application's event, like BeginRequest and EndRequest.
To register, you need to add the following in [cci]Web.config[/cci]:
This module will cause each page returned from the server to be decorated with a header and a footer.
ASP.NET provides built-in modules, which are a part of standard processing. There modules are listed in the framework's [cci]Web.config[/cci] placed in [cci]c:\Windows\Microsoft.NET\Framework\v4.0.30319\[/cci] folder.
[cci]HTTPApplication[/cci] class has a property [cci]Modules[/cci] which contains all the loaded modules. To see them just set a breakpoint inside [cci]Global.asax[/cci] and investigate Watch window.
An ASP.NET HTTP handler is the process that runs in response to a request made to an ASP.NET Web application. It is slightly different than a module. Handlers are sometimes referred to as "endpoints". A handler can be associated with file extension. E.g. file extension [cci].aspx[/cci] is assigned to [cci]System.Web.UI.PageHandlerFactory[/cci] class, which handles standard ASP.NET page processing. In this Stack Overflow question there is a good two-sentence explanation of the difference between HTTP Module and HTTP Handler: HttpHandler is where the request train is headed. HttpModule is a station along the way. So when we request ASPX page all the registered modules will be initialized and only one HTTP handler will be run.
Creating custom HTTP handler is easy as it was with HTTP modules. You should implement [cci]IHttpHandler[/cci] interface and register handler in [cci]Web.config[/cci] class.
To see which HTTP handler is currently being run in the current request let's see standard ASP.NET and ASP.NET MVC application. We need to set a breakpoint in code-behind class for the first and in a controller for the second. There result are as below.
The second of these is [cci]MvcHandler[/cci], but the first shows that our handler is actually our ASP.NET page. Let's look at [cci]WebForm1[/cci] class. It inherits from [cci]System.Web.UI.Page[/cci] which inherits from [cci]TemplateControl[/cci] and implements [cci]IHttpHandler[/cci] interface. It means that each ASP.NET page is actually a HTTP handler itself. But how the process know about our handler, while the framework's [cci]Web.config[/cci] file contains:
The [cci]PageHandlerFactory[/cci] class creates an instance of our [cci]WebForm1[/cci] class based on the request's URL.