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

Advertisements

Written by Abhijit Jana

Technology Leader, Technical Strategist, Solution Architect, Development Consultant and a Trusted Technology Advisor with more than 11.5 years of experience in the IT industry with expertise in Development, Architecting, Engineering, Consulting, and Services Delivery. I am a former Microsoft MVP, Author of "Kinect for Windows SDK Programming Guide" and "HoloLens BluePrints" books and founder of The Daily .NET Tips . To know more about me visit "About Me" page.

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: