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.
- HttpHandler and HttpModule in Real Scenario: Getting Started with HttpHandler
- HttpHandler and HttpModule in Real Scenario: Few examples of HttpHandler-1
- Working with Image in HttpHandler
- Working With Header in HttpHandler
- HttpModule in real scenario: Implement simple HttpModule
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.
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Linq;
- using System.Web;
- using System.Xml.Linq;
- using Newtonsoft.Json;
- namespace WebApp
- {
- public class MyHttpModule1 : IHttpModule
- {
- public void Dispose()
- {
- throw new NotImplementedException();
- }
- public void Init(HttpApplication context)
- {
- HttpContext.Current.Items.Add("M1", "This is module 1");
- }
- }
- public class MyHttpModule2 : IHttpModule
- {
- public void Dispose()
- {
- throw new NotImplementedException();
- }
- public void Init(HttpApplication context)
- {
- HttpContext.Current.Items.Add("M2", "This is module 2");
- }
- }
- }
- <system.webServer>
- <modules>
- <add name="mymodule1" type="WebApp.MyHttpModule1"/>
- <add name="mymodule2" type="WebApp.MyHttpModule2"/>
- </modules>
- <system.webServer>
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace WebApp
- {
- public partial class WebForm1 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- { Response.Write(HttpContext.Current.Items["M1"] + "</br>");
- Response.Write(HttpContext.Current.Items["M2"]);
- }
- }
- }

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.
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Linq;
- using System.Web;
- using System.Xml.Linq;
- using Newtonsoft.Json;
- namespace WebApp
- {
- public class MyHttpModule1 : IHttpModule
- {
- public void Dispose()
- {
- throw new NotImplementedException();
- }
- public void Init(HttpApplication context)
- {
- HttpContext.Current.Items.Add("M1", "This is module 1");
- }
- }
- public class MyHttpModule2 : IHttpModule
- {
- public void Dispose()
- {
- throw new NotImplementedException();
- }
- public void Init(HttpApplication context)
- {
- String value = HttpContext.Current.Items["M1"].ToString();
- }
- }
- }
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.

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.
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Linq;
- using System.Web;
- using System.Xml.Linq;
- using Newtonsoft.Json;
- namespace WebApp
- {
- public class MyHttpModule1 : IHttpModule
- {
- public void Dispose()
- {
- throw new NotImplementedException();
- }
- public void Init(HttpApplication context)
- {
- //HttpContext.Current.Items.Add("M1", "This is module 1");
- context.BeginRequest += new EventHandler(begin_request);
- }
- public void begin_request(Object sender, EventArgs e)
- {
- HttpApplication application = (HttpApplication)sender;
- HttpContext context = application.Context;
- context.Response.Write("<h1><font color=red>"+ "Response from Module1"+ "</font></h1><hr>");
- }
- }
- }
Here is the output of the example above.

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.

Smith BlackPosted Oct 21, 2021, 6:23 PM
Hi, Thanks for your nice post, but for the last example, what happens if we have multiple modules with binding to their respective app_beginRequest()? Thanks