Channels ▼
RSS

Web Development

Less Intrusive HTTP Modules



An HTTP module is a .NET class that implements the IHttpModule interface. Once registered with an ASP.NET Web application, such classes hook up any incoming HTTP request and gain full access to the headers and raw data in the packet. All ASP.NET applications inherit a bunch of system HTTP modules as configured in the system's web.config file. An HTTP module can pre-process and post-process a request, and it intercepts and handles system events as well as events raised by other modules.

HTTP modules are extremely powerful tools as through them you get yourself in total control of the request and close to the nerve center of ASP.NET applications. This strength, though, is also a major weakness of HTTP modules. If abused or misused, in fact, HTTP modules can end up taxing any incoming requests and suddenly transform into the perfect bottleneck for your application.

Unlike HTTP handler, HTTP modules are not bound to a specific extension or a family of URLs. In a certain sense, HTTP modules are lower level tools than handlers and are loaded at the very startup of the application. HTTP modules form a pipeline that each Web request passes through. Modules are used to hook up application-level events rather than specific requests. Here's a typical implementation of a HTTP module:

public class SampleModule : IHttpModule
{
    public void Init(HttpApplication app)
    {
        // Register for pipeline events
    }
    public void Dispose() 
    {
        // Nothing to do here
    }
}

The Init module is invoked at the application startup and registers the module with the application events of interest -- BeginRequest, AuthenticateRequest, AcquireRequestState, EndRequest, and the like. The Dispose method is invoked when the application is shutting down and is normally used to dispose of any resources that was acquired earlier. An HTTP module is used to authenticate and authorize a request, to load and save the session state, to deal with the output cache. As you can see, a HTTP module is the ideal tool to implement cross-cutting concerns of an application when not extensions to the ASP.NET platform.

It may happen that your application needs to implement some sort of pre- and post-processing on top of a certain category of requests. The HTTP module is seemingly the perfect to tool; but unfortunately, once registered, it kicks in for each and every request that arrives to your Web server and is dispatched to your ASP.NET application. For example, no JPEG file requests triggers a module because this request is served directly by IIS and never reaches the ASP.NET runtime. If, say, you need to pre-process only ASCX requests than a HTTP module might be optimal if it weren't that it would hook up also ASPX or ASMX requests.

Is there a way to fix this issue? I'm afraid this aspect of HTTP modules is built this way by design. One thing you can certainly do is, however, hook up the BeginRequest event and check whether the current request is something you may be interested in. If this is not the case, then switch off some internal flag that will prevent the rest of HTTP module's code to work for the duration of that request.

It's not a definitive answer, but it helps at least a little bit.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 
Dr. Dobb's TV