Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpModule not called in .NET 4.5

Tags:

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

like image 242
timprice Avatar asked Apr 02 '13 15:04

timprice


People also ask

What is the difference between middleware and HttpModule?

Unlike HttpModules, there is full control of what get's executed and in what order. As they are executed in the order in which they are added. Order of middleware for responses is the reverse from that for requests. Middleware is independent of these events.

What is HTTP handler and HttpModule in MVC?

To explain HTTP Modules and HTTP Handlers, HTTP module and HTTP handler are used by MVC to inject pre-processing logic in the request chain. HTTP Handlers are extension based pre-processor whereas HTTP Module are event based preprocessor.

What is the difference between HTTP handlers & HTTP modules?

Handlers enable the ASP.NET framework to process individual HTTP URLs or groups of URL extensions within an application. Unlike modules, only one handler is used to process a request. All handlers implement the IHttpHandler interface, which is located in the System. Web namespace .

What is the use of HttpModule?

An HttpModule is a component that is part of the ASP.NET request processing pipeline and is called on every request that is made to your application. Note that HttpModules can have access to the life cycle events of a request and hence they can be used to modify the response as well.


1 Answers

<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" /> 
like image 66
robrich Avatar answered Oct 10 '22 02:10

robrich