Enumerating HttpModules

Posted August 24, 2008 in asp.net performance
Reading time: 1 minute

One common ASP.NET performance tip is to remove any HttpModules that your application does not use.  You can take a peek at which modules are loaded by the framework on your behalf by examining the framework’s Web.config file, but how do you find out which modules are actually loaded in the current context?

Fortunately, the HttpApplication instance provides a Modules collection that you can loop through:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// Get a collection of modules loaded for the current context.
HttpModuleCollection modules = HttpContext.Current.ApplicationInstance.Modules;

// Enumerate the loaded HttpModules and do something with them.  For example, you can
//  create a table, listing each module by key and full type name.
// NOTE: the key is the same key specified in Web.config to add the module.
foreach (string moduleKey in modules.Keys)
{
    IHttpModule module = modules[moduleKey];

    // Do something with the module key and/or the HttpModule.
}

You can then use the Modules collection create a table like this:

Enumerated HttpModules
Enumerated HttpModules

And boom, there you have it: a list of HttpModules that are loaded for the current context.  You can use this information to determine which HttpModules are needed by your application, and which ones you can safely remove.



Comments

comments powered by Disqus