Search code examples
c#.netasp.nethttpmodule

How should I reference an IHttpModule instance in the pipeline?


An IHttpModule implementation I created

public class ResponseTweaker : IHttpModule {  // my module ...

is registered in Web.config

<system.web>
  <httpModules>
      <add name="redman" type="ResponseTweaker"/>
  </httpModules>

and an instance of it is sitting in the pipeline.

From the perspective of a caller (e.g. from the Global.asax.cs file), how should I get a reference to that module instance?


Solution

  • The modules can be found in HttpContext.Current.ApplicationInstance.Modules.

    You can look through the HttpModuleCollection, or use the name syntax: HttpContext.Current.ApplicationInstance.Modules["redman"]. You will probably need to cast the returned IHttpModule to type ResponseTweaker.

    If the type may vary, search the collection to find module(s) matching the desired type.