Enumerating HttpModules: MVC Edition

Posted August 13, 2014 in asp.net-mvc httpmodule
Reading time: 1 minute

About 6 years ago, I wrote a post about Enumerating HttpModules in ASP.NET. On my current project, I once again needed to view the loaded HttpModules, but this time in ASP.NET MVC. The code is very similar; it just has some MVC-isms and has been LINQ-ified now.

Here is the relevant code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
public ActionResult Index()
{
    // Get the modules for the current application.
    var modules = HttpContext.ApplicationInstance.Modules;

    // Convert the collection of keys to an IEnumerable, and then use the keys to
    // index into the list of modules to create a dictionary.
    // * Key = module key name
    // * Value = module full type name
    var loadedModules = modules
        .Cast()
        .Select(moduleKey => new { Key = moduleKey, Module = modules[moduleKey] })
        .ToDictionary(m => m.Key, m => m.Module.GetType().FullName);

    return View(new HomeIndexModel(loadedModules));
}

And here is what the output would look like in the default ASP.NET MVC Bootstrap template:

Loaded HttpModules
Loaded HttpModules

The sample code is available on GitHub: EnumerateMvcHttpModules

Happy enumerating!

Share on:


Comments

comments powered by Disqus