How to retrieve HTTPModule details from HttpModuleCollection ?

In one of my previous post I have talked about How to get list of all active HttpModules in ASP.NET? Where I have explained how we can get list of all active modules of an ASP.NET Application at runtime. In this post, I am going discussed about, how you can get details of a particular module details from list of modules.

when a request reaches to worker Process from client browser, first of all Worker process is responsible to start HttpRuntime by loading ISAPI filter. After that HttpRuntime load an HttpApplication object with the help of HttpApplicationFactory class. Each and every request should pass through the corresponding HTTPModule to reach to HTTPHandler, this list of module are configured by the HTTPApplication. HttpContext.Current.ApplicationInstance will give us the HTTPApplication object for current Request. This HTTPApplication object contains the list of modules.

          //Get Application Instance from Current Content
            HttpApplication httpApps = HttpContext.Current.ApplicationInstance;
            //Get List of modules in module collections
            HttpModuleCollection httpModuleCollections = httpApps.Modules;

Now we run the below code block, we will get list of all active modules,

  foreach (string activeModule in httpModuleCollections.AllKeys)
            {
                Response.Write(activeModule + "</br>");
            }

List of Modules Output :

image

Now, if you want to get details of any HTTPModule, you have to use httpModuleCollections.Get(“<ModuleName>”). This will return you IHttpModule Object. Now from the return object you can get details of that module.

Let’s consider, if you want to get details of “Session” module, you have to write below code block.

IHttpModule sessionModule = httpModuleCollections.Get("Session");

If you try to inspect the sessionModule object in Quick Watch window, you will get all the details related with Session Module

image

httpModuleCollections.Get() will also help you to get details of any of your custom module, moreover if you want to call any method from your Custom HTTPModule you have to use the same approach.

IHttpModule customModule = httpModuleCollections.Get("MyCustomModule");
ICustomModule myCustomModule = (ICustomModule)customModule;
string result = myCustomModule.ModuleMethod()

Hope this will help !

Cheers !!

AJ

Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.