HttpModule in Real Scenario: Multiple HttpModule and Communication

This article series is explaining HttpHandler and HttpModule. The purpose of this article is to understand the use HttpHandler and HttpModule in real time software development. In our previous few articles, we have learned various concepts of HttpHandler. You can read them here.

In our previous article I said that we can implement multiple HttpModules in a single ASP.NET application and the module will execute according to its registration.

Add multiple HttpModules to an ASP.NET request and response pipeline

In our HttpHanlder discussion we saw that there is only one HttpHandler to process a certain type of resource. So HttpHandler is called an endpoint. But in the case of HttpModule, the scenario is not the same. We can set any number of HttpModules in an ASP.NET Http request and response pipeline. In the following example we will add multiple HttpModules. Have a look at the following code.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Drawing;  
  4. using System.Drawing.Imaging;  
  5. using System.IO;  
  6. using System.Linq;  
  7. using System.Web;  
  8. using System.Xml.Linq;  
  9. using Newtonsoft.Json;  
  10. namespace WebApp  
  11. {  
  12.     public class MyHttpModule1 : IHttpModule  
  13.     {  
  14.         public void Dispose()  
  15.         {  
  16.             throw new NotImplementedException();  
  17.         }  
  18.         public void Init(HttpApplication context)  
  19.         {  
  20.             HttpContext.Current.Items.Add("M1""This is module 1");  
  21.         }  
  22.     }  
  23.     public class MyHttpModule2 : IHttpModule  
  24.     {  
  25.         public void Dispose()  
  26.         {  
  27.             throw new NotImplementedException();  
  28.         }  
  29.         public void Init(HttpApplication context)  
  30.   
  31.         {  
  32.             HttpContext.Current.Items.Add("M2""This is module 2");  
  33.         }  
  34.     }   
  35. } 
Fine, we have declared two classes and implemented an IHttpModule in each one of them. We will now register those two modules in the web.config file. Just add two HttpModules, one by one. The execution sequence will depend on it’s registration sequence.

  1. <system.webServer>  
  2.       <modules>  
  3.         <add name="mymodule1" type="WebApp.MyHttpModule1"/>  
  4.         <add name="mymodule2" type="WebApp.MyHttpModule2"/>       
  5.       </modules>  
  6. <system.webServer> 

 

Now, in the form1.aspx page we are accessing the Item[] collection that we have set in HttpModule.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. namespace WebApp  
  8. {  
  9.     public partial class WebForm1 : System.Web.UI.Page  
  10.     {  
  11.         protected void Page_Load(object sender, EventArgs e)  
  12.         {  Response.Write(HttpContext.Current.Items["M1"] + "</br>");             
  13.            Response.Write(HttpContext.Current.Items["M2"]);  
  14.         }  
  15.     }  
  16. } 

 

Here is the output of the example above.

Add multiple HttpModules

Pass information from one HttpModule to another HttpModule

This is an important question: when we want to implement more than one HttpModule in a single application, how will we pass data from one module to another module. One of the best ways to pass data from one module to another module is with an Item[] collection. In this example we have initialized something in an Item[] collection and from the second controller we are trying to access it.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Drawing;  
  4. using System.Drawing.Imaging;  
  5. using System.IO;  
  6. using System.Linq;  
  7. using System.Web;  
  8. using System.Xml.Linq;  
  9. using Newtonsoft.Json;  
  10. namespace WebApp  
  11. {  
  12.     public class MyHttpModule1 : IHttpModule  
  13.     {  
  14.         public void Dispose()  
  15.         {  
  16.             throw new NotImplementedException();  
  17.         }  
  18.         public void Init(HttpApplication context)  
  19.         {  
  20.             HttpContext.Current.Items.Add("M1""This is module 1");  
  21.         }  
  22.     }  
  23.     public class MyHttpModule2 : IHttpModule  
  24.     {  
  25.         public void Dispose()  
  26.         {  
  27.             throw new NotImplementedException();  
  28.         }  
  29.         public void Init(HttpApplication context)  
  30.   
  31.         {  
  32.             String value = HttpContext.Current.Items["M1"].ToString();  
  33.         }  
  34.     }   
  35. } 

The implementation is very simple. In one HttpModule we are setting the item[] collection's value and in another HttpModule are accessing it.

Here is the output for when we are trying to access it from another HttpModule.

Pass information from one HttpModule to another HttpModule

Make HttpResponse from HttpModule

We sometimes might need to generate a Http response from a HttpModule itself. The scenario might arise in a security implementation. We can generate an illegal access response if any HTTP request/ response breaks our security implementation.

In this example we are generating a HTTP response message from a HttpModule itself.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Drawing;  
  4. using System.Drawing.Imaging;  
  5. using System.IO;  
  6. using System.Linq;  
  7. using System.Web;  
  8. using System.Xml.Linq;  
  9. using Newtonsoft.Json;  
  10. namespace WebApp  
  11. {  
  12.     public class MyHttpModule1 : IHttpModule  
  13.     {  
  14.         public void Dispose()  
  15.         {  
  16.             throw new NotImplementedException();  
  17.         }  
  18.         public void Init(HttpApplication context)  
  19.         {  
  20.             //HttpContext.Current.Items.Add("M1", "This is module 1");  
  21.            context.BeginRequest += new EventHandler(begin_request);  
  22.         }   
  23.         public void begin_request(Object sender, EventArgs e)  
  24.         {  
  25.             HttpApplication application = (HttpApplication)sender;  
  26.   
  27.             HttpContext context = application.Context;  
  28.   
  29.             context.Response.Write("<h1><font color=red>""Response from Module1""</font></h1><hr>");  
  30.         }  
  31.     }  
  32. } 

Here is the output of the example above.

HttpResponse from HttpModule

Conclusion

In this example, we have learned how to implement more than one HttpModule in an ASP.NET application and also learned their execution process. Though this is not a realistic problem implementation using HttpModule but for a basic understanding this will help us to understand more complex examples. The next article will discuss various events of HttpModule.


Similar Articles