Tag Archives: Performance

Enumerating HttpModules

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:

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

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.