HttpModule not called in .NET 4.5

timprice picture timprice · Apr 2, 2013 · Viewed 10.6k times · Source

Just spent a lot of time sifting through contradictory advice on this problem, and thought I'd post my solution.

My environment is .NET 4.5, Visual Studio 2012, working on an MVC 4 application. I created an Http Module like I'd done in the past, and added it to Web.config like so:

<configuration>
 <system.web>
  <httpModules>
   <add name="MyModule" type="Services.MyModule, Services" />
  </httpModules>
 </system.web>
</configuration>

However, the application never called the module's Init(). Eventually, I found advice that the modules should instead be inside <system.webServer>, and the element named <modules> instead of <httpModules>, like so:

<configuration>
 <system.webServer>
  <modules>
   <add name="MyModule" type="MyModule" type="Services.MyModule, Services" />
  </modules>
 </system.webServer>
</configuration>

Re-ran the application, and it called Init() as expected. FWIW, the page with the direction is here: http://msdn.microsoft.com/en-us/library/ms227673.aspx

HTH

Answer

robrich picture robrich · Aug 26, 2013

<system.web> is for IIS 6 and below (including Cassini), <system.webServer> is for IIS 7 and above. I generally put in both -- just in case -- and then add this node to <system.webServer> so it doesn't barf on the redundancy:

<validation validateIntegratedModeConfiguration="false" />