Pass Data From HttpModule to HttpHandler in ASP.Net Application

In our previous articles in this series we have learned various concepts of HttpHandler and HttpModule in an ASP.NET request and response pipeline. You can read them here.

In this small article we will learn to pass data from a HttpModule to a HttpHandler. Before getting to the example implementation, let’s clearify a few concepts.

  • We can implement more than one HttpModule in a request and response pipeline.
  • There will be only one HttpHandler for one type of resource.
  • HttpModule will execute before HttpHandler.
  • If more than one HttpModule is present in the pipeline then they will execute depending on their registration sequence.
  • We can write more than one HttpModule and one HttpHandler (for one type resource) in a single .ashx page.

Fine, we have implemented those concepts pragmatically in all our previous articles (I recommend you visit our previous articles if you are not clear about those concepts).

In this article we will learn to pass data from a HttpModule to a HttpHandler. Sometimes it might be necessary to pass some information between a HttpModule and a HttpHandler and in this scenario this article might be a good resources to solve the problem.

We know that an Item[] collection is for a one-time request and response process. In other words the collection value will be retained during a single HttpRequest or HttpResponse message.

And in our scenario, an item[] collection is the best possible solution (at least, I hope; if a better solution exists then please let us know in a comment).

Yes, we will use an Item[] collection to send data from a HttpModule to a HttpHandler. Have a look at the following example.

  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 MyHttpHandler1 : IHttpHandler  
  13.     {  
  14.         public void ProcessRequest(HttpContext context)  
  15.         {  
  16.             if (HttpContext.Current.Items["Item"] != null)  
  17.             {  
  18.                 context.Response.Write("Page from HttpHandler with Item data " +  
  19.                     HttpContext.Current.Items["Item"]);  
  20.             }  
  21.         }  
  22.         public bool IsReusable  
  23.         {  
  24.             get  
  25.             {  
  26.                 return false;  
  27.             }  
  28.         }   
  29.     }  
  30.     public class MyHttpModule1 : IHttpModule  
  31.     {  
  32.         public void Dispose()  
  33.         {  
  34.             throw new NotImplementedException();  
  35.         }  
  36.         public void Init(HttpApplication context)  
  37.         {  
  38.             context.AcquireRequestState += new EventHandler(AcquireRequestState);  
  39.         }  
  40.         public void AcquireRequestState(Object sender, EventArgs e)  
  41.         {  
  42.             HttpContext Context = ((HttpApplication)sender).Context;  
  43.             Context.Items.Add("Item""Item Data");  
  44.         }  
  45.     }  
  46. } 

The Item Collections stores data in a key/value pair, just like a session. We now need to register both the HttpModule and the HttpHandler.

Put the following code into your web.config file. It does not matter whether HttpModule or HttpHandler is registered first. The HttpModule will always execute before the HttpHandler.

  1. <system.webServer>  
  2.       <modules>  
  3.         <add name="mymodule1" type="WebApp.MyHttpModule1"/>  
  4.       </modules>  
  5.       <handlers>  
  6.        <add name="myHandler" verb="*" path="*.aspx" type="WebApp.MyHttpHandler1"/>  
  7.       </handlers>  
  8.     <validation validateIntegratedModeConfiguration="false"/>  
  9. </system.webServer> 

Here is the output. We are getting value from Item[] collection.

Item collection

Conclusion

In this small article, we have learned to pass a value from a HttpModule to a HttpHandler. I hope this will help when you try something cool with both HttpModule and HttpHandler. In a future article we will understand a few more pragmatic uses of HttpModule.


Similar Articles