The more I go out and do presentations about ASP.NET tactics the more I seem to find myself demonstrating the use of httpModules and httpHandlers. I wanted to give a brief introduction to httpModules for everyone. Especially since I have already been discussing ASP.NET httpModules here and have several that will be published very soon.
HttpModules are simply classes that can reside either in your Web site's code or in a separate class library. They are plugged into the life cycle of each request run through the ASP.NET engine. Typically they hook into events in the request life cycle and can react to actual request or modify the output in the Response object before it is sent to the client.
Each httpModule must be registered with the site in the web.config file. This is done in the system.web section through the httpModules element. This example show a typical registration for one of my current sites. I have two modules built into the site and then the ASP.NET AJAX module registration.
<add type='RegExUrlMapping_HTTPModule.RegExUrlMappingModule' name='RegExUrlMappingModule'/>
<add name='ScriptModule' type='System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'/>
<add type='_301RedirectModule' name='_301RedirectModule'/>
To create your own httpModule you must define a class that implements the IhttpModule interface. This interface requires you implement two methods, Init and Dispose. In the Init method you typically will wire up event handlers for one or more events in the Page life cycle, such as AuthenticateRequest. You can learn more about the ASP.NET page life cycle in this detailed MSDN article.
The httpModule's Init method passes a reference to the current httpApplication object. This provides access to the current application, and the ASP.NET page life cycle events. These events can be wire to event handlers in the module. Each event handler must have the standard ASP.NET event signature, a sender as an Object and and EventArgs object. You can add any custom code you would like to your event handler, just be aware that some objects may not have been instantiated or might have been destroyed at the various points of the life cycle.
An example from a Previous post on 301 redirects
_context = context
AsHashtable = Get301Mappings()
String= _ app.Context.Request.AppRelativeCurrentExecutionFilePath
app.Response.StatusCode = 301
' make a permanent redirect
The Module's Dispose method allows you to clean up any resources that need to be destroyed before the application closes. Typically you will want to remove any event handlers, close any open files, etc.
Now most of what you can do with an httpModule can be done in the Global.asax file. The main difference between the two is that common functionality can be encapsulated in an httpModule and distributed among various sites and even placed in the GAC. The advantage the Global.asax has is that it is easier to work with Session level events.
I encourage you to read my entries on practical application of httpModules, the MSDN articles and any other online resources for httpModules you can find. Using modules can be a great benefit to making your site more professional, easier to maintain and running efficiently.