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.

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 MyHttpHandler1 : IHttpHandler

    {

        public void ProcessRequest(HttpContext context)

        {

            if (HttpContext.Current.Items["Item"] != null)

            {

                context.Response.Write("Page from HttpHandler with Item data " +

                    HttpContext.Current.Items["Item"]);

            }

        }

        public bool IsReusable

        {

            get

            {

                return false;

            }

        } 

    }

    public class MyHttpModule1 : IHttpModule

    {

        public void Dispose()

        {

            throw new NotImplementedException();

        }

        public void Init(HttpApplication context)

        {

            context.AcquireRequestState += new EventHandler(AcquireRequestState);

        }

        public void AcquireRequestState(Object sender, EventArgs e)

        {

            HttpContext Context = ((HttpApplication)sender).Context;

            Context.Items.Add("Item", "Item Data");

        } 

    }  

}

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.

<system.webServer>

      <modules>

        <add name="mymodule1" type="WebApp.MyHttpModule1"/>

      </modules>     

      <handlers>

       <add name="myHandler" verb="*" path="*.aspx" type="WebApp.MyHttpHandler1"/>

      </handlers>

    <validation validateIntegratedModeConfiguration="false"/>

</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.
 

Up Next
    Ebook Download
    View all
    Learn
    View all