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.
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");
}
}
}
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.
<system.webServer>
<modules>
<add name="mymodule1" type="WebApp.MyHttpModule1"/>
<add name="mymodule2" type="WebApp.MyHttpModule2"/>
</modules>
<system.webServer>
Now, in the form1.aspx page we are accessing the Item[] collection that we have set in HttpModule.
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"]);
}
}}
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.
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.
![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.
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.
![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.