Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Chart
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
DevExpress UI Controls
Search :       Advanced Search »
Home » ASP.NET & Web Forms » HTTP Modules in ASP.NET Request life cycle

HTTP Modules in ASP.NET Request life cycle

HTTP module is a .NET Framework class that implements the IHttpModule interface, they called as ISAPI filter in IIS stack so modules are counterpart in ASP.NET, have ability to intercept and handle system events and other modules events.

Author Rank :
Page Views : 4678
Downloads : 55
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
SampleModules.zip
 
 
DevExpress Free UI Controls
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


HTTP module is a .NET Framework class that implements the IHttpModule interface, they called as ISAPI filter in IIS stack so modules are counterpart in ASP.NET, have ability to intercept and handle system events and other modules events. Based on the web.config file, per application basis, HTTP modules can filter the raw data, the context, within the request.

Note: all ASP.NET applications inherit a gang of system modules by default as configured in the global web.config  

Before we get in to the details of internals let us understand IHttpModule interface and HTTPApplication or ASP.NET Application object first.

ASP.NET Application object (HTTPApplication)

In classic pipeline mode an ASP.NET request is handed over to an ISAPI extension right after IIS has obtained an authentication token for the sender.  An ISAPI extension, HTTP Handler is ASP.NET counterpart, is a Win32 DLL that gets loaded into the IIS worker process that's in charge for any given Web application. The ISAPI extension will load the CLR in the worker process and launch the ASP.NET runtime pipeline to actually process the request.

In the ASP.NET pipeline, the request life cycle is governed by a static instance of the HttpRuntime class. A single instance of the HttpRuntime class exists per application, and it's created when the first request for the application comes in. When the HttpRuntime object is takes over the control to process a request, it performs a number of initialization tasks the first one is the creation of the HTTP context object (which contains the ASP.NET intrinsic  objects like Response, Request, Session and many more) and followed by creation of an ASP.NET application object to carry out the request. 

So by now we have Worker Process, CRL up and running, HttpRuntime object, HttpContext object, HttpApplication object. ASP.NET application object uses newly created HttpContext object to complete the request. Now given ASP.NET application object let us see where and all and what and all we can do. 

Don't forget "many more" we will see them. They are the concern of this topic. Before that, let us see what IHttpModule is first.

IHttpModule interface

public interface IHttpModule
    void Dispose();
    void Init(HttpApplication context);
}

The IHttpModule interface has two methods namely Init and Dispose. Any class that inherits from IHttpModule needs to provide the implementation for these methods. As we all know that Dispose method takes care of resource cleanup typically closing DB connection, any handlers to the costly disk resources. We have lest concern about this method. Let us see what can, or have to, do with the Init method. 

Init method, has one parameter that is HttpApplication Object, at first initializes the http module and prepares for handling requests. It receives the HttpApplication object reference when it executes. As said now http module has the access to all intrinsic objects and many more.

Now we will see what is 'many more', the HttpApplication object has many events in it, below is the list. These events can be wired up in the HttpModule and providing the behavior for those events. So by using HttpModule we can bypass or customize the behavior for the application by wiring up these events in the custom modules.    

EventDescription
AcquireRequestState, PostAcquireRequestStateOccurs when the handler that will actually serve the request acquires the state information associated with the request.
AuthenticateRequest, PostAuthenticateRequestOccurs when a security module has established the identity of the user. 
AuthorizeRequest, PostAuthorizeRequestOccurs when a security module has verified user authorization. 
BeginRequestOccurs as soon as the HTTP pipeline begins to process the request. 
DisposedOccurs when the HttpApplication object is disposed of as a result of a call to Dispose. 
EndRequestOccurs as the last event in the HTTP pipeline chain of execution. 
Error Occurswhen an unhandled exception is thrown.
LogRequest, PostLogRequestOccurs when the response has been generated and logging modules can do their work. These events are fired only to applications that run in Integrated pipeline mode under IIS 7. 
MapRequestHandlerOccurs when it is about time to set the handler to serve the request. This event is fired only to applications that run in Integrated pipeline mode under IIS 7.
PostMapRequestHandlerOccurs when the HTTP handler to serve the request has been found. 
PostRequestHandlerExecuteOccurs when the HTTP handler of choice finishes execution. The response text has been generated at this point.
PreRequestHandlerExecuteOccurs just before the HTTP handler of choice begins to work.
PreSendRequestContent Occurs just before the ASP.NET runtime sends the response text to the client. 
PreSendRequestHeaders Occurs just before the ASP.NET runtime sends HTTP headers to the client. 
ReleaseRequestState, PostReleaseRequestStateOccurs when the handler releases the state information associated with the current request.
ResolveRequestCache, PostResolveRequestCacheOccurs when the ASP.NET runtime resolves the request through the output cache. 
UpdateRequestCache, PostUpdateRequestCacheOccurs when the ASP.NET runtime stores the response of the current request in the output cache to be used to serve subsequent requests. 

Now let us implement and see the HttpModule in action. For the implementation I am using trail version of Microsoft Visual Studio 2010 Ultimate. Follow the steps to create the sample model in ASP.NET application.

1. Open up VSTS and create an ASP.NET Web Application using default template. Names of solution and web application at your convenience. By default application is created with many folders in it, I am deleting all of them including default page for ease purpose and web.config entries.

2. Add an ASP.NET Module to the solution. And it looks like below.

using System;
using System.Web;

namespace SampleModelule
{
    public class SampleModule : IHttpModule
    {
        /// <summary>
        /// You will need to configure this module in the web.config file of your
        /// web and register it with IIS before being able to use it. For more information
        /// see the following link: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpModule Members
        public void Dispose()
        {
            //clean-up code here.
        }
        public void Init(HttpApplication context)
        {
            // Below is an example of how you can handle LogRequest event and provide
            // custom logging implementation for it
            context.LogRequest += new EventHandler(OnLogRequest);
        }
        #endregion
        public void OnLogRequest(Object source, EventArgs e)
        {
            //custom logging logic can go here
        }
    }
}

3. Clean up the default code and add the following simple code.

public void Init(HttpApplication context)
{
    context.BeginRequest += new EventHandler(OnBeginRequest);
    context.EndRequest += new EventHandler(OnEndRequest);
}
void OnEndRequest(object sender, EventArgs e)
{
}
void OnBeginRequest(object sender, EventArgs e)
{
}

4. The events handlers BeginRequest & EndRequest have a similar signature. They obtain a reference to the current HttpApplication object from the sender and get the access to the all the intrinsic objects from there. Next, they work with the Response object to append custom text to the response header.

5. Add the following code to the methods.

public void Init(HttpApplication context)
{
    context.BeginRequest += new EventHandler(OnBeginRequest);
    context.EndRequest += new EventHandler(OnEndRequest);
}
void OnEndRequest(object sender, EventArgs e)
{
    HttpApplication appObject = (HttpApplication)sender;
    HttpContext contextObject = appObject.Context;
    // PageHeaderText is a constant string defined elsewhere
    contextObject.Response.Write("<table width='100%' border='2'><tr><td align='center' background='Gray'>The last event in the HTTP pipeline chain of execution</td></tr></table>");
}
void OnBeginRequest(object sender, EventArgs e)
{
    HttpApplication appObject = (HttpApplication)sender;
    HttpContext contextObject = appObject.Context;
    // Add custom header to the HTTP response
    contextObject.Response.AppendHeader("Author", "Chinna Srihari");
    // PageHeaderText is a constant string defined elsewhere
    contextObject.Response.Write("<table width='100%' border='2'><tr><td align='center' background='Gray'>HTTP pipeline begins to process the
request</td></tr></table>");
}

Now we have the module is ready to use and the time for registering the module. To register any module we should know the scope. If any module that is being used should use across all the ASP.NET applications the go for the global web.config this change will affect all the applications, ASP.NET, in the server. Example is authentications. In any case develop and test the module in any one of applications first. For this reason use the web.config file. 

Following is the section we will be using to deploy the HttpModule in ASP.NET applications.

<system.web>
      <httpModules>
      <add name="MyFirstModule"
           type="Chinna.SampleModules.SampleModule, SampleModuleAssembly"/>
    </httpModules>
</system.web>

Name --> any name that you feel publically used
Type --> it has got two things, fully qualified class name and asseblyname by comma separated.  

That's it. Just run the application and you see as below.

1.gif

Now let us see what else we can do more, URL Rewriting.

Let us understand. From the browser we will be requesting a page to process and display. But there must be other page that actually serves the request, but at the browser the original URL remains same. This kind of behavior or functionality is achieved by using HTTPContext Object's RewriteUrl method. 

See the below example, the same above code I am using. Add a new page to the application called WebForm1.aspx. Add the some text like "<h2>Chinna! this is routed page, not your original page</h2>". Whereas we have different text on the Default.aspx "<h2>Chinna! this is routed page, not your original page</h2>"

Just add the below code to your at the end you your OnBeginrequest method.

contextObject.RewritePath("~/WebForm1.aspx");

So you method becomes like 

void OnBeginRequest(object sender, EventArgs e)
{
    HttpApplication appObject = (HttpApplication)sender;
    HttpContext contextObject = appObject.Context;
    // Add custom header to the HTTP response
    contextObject.Response.AppendHeader("Author", "Chinna Srihari");
    // PageHeaderText is a constant string defined elsewhere
    contextObject.Response.Write("<table width='100%' border='2'><tr><td align='center' background='Gray'>HTTP pipeline begins to process the request</td></tr></table>");
    contextObject.RewritePath("~/WebForm1.aspx");
}

Now run the application, see the screen below

2.gif

Note: there is a new model in VSTS 2008 with SP1, but still this works

Hope this helps.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Srihari Chinna
Good experience in designing the applications and Architecting Enterprise application using SharePoint 2007 technologies and Service Oriented applications using ASP.NET and WCF Services. Also had hands on experience in designing in .NET Web based application using ASP.NET and AJAX.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
DevExpress Free UI Controls
Become a Sponsor
 Comments

 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.