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
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.
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.
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 .
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.
<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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With