Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpModule with ASP.NET MVC not being called

I am trying to implement a session-per-request pattern in an ASP.NET MVC 2 Preview 1 application, and I've implemented an IHttpModule to help me do this:

public class SessionModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.Response.Write("Init!");
        context.EndRequest += context_EndRequest;
    }
    // ... etc...
}

And I've put this into the web.config:

  <system.web>
    <httpModules>
      <add name="SessionModule" type="MyNamespace.SessionModule" />
    </httpModules>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="SessionModule" />
      <add name="SessionModule" type="MyNamespace.SessionModule" />
    </modules>

However, "Init!" never gets written to the page (I'm using the built-in VS web server, Cassini). Additionally, I've tried putting breakpoints in the SessionModule but they never break. What am I missing?

like image 771
Matthew Groves Avatar asked Sep 07 '09 21:09

Matthew Groves


2 Answers

Turns out there's a Web.config file in the Views folder, and one in the root. Guess which one I registered the httpModules in? Yep, the Views folder one. I moved it to the root Web.config and now it works like a charm.

like image 51
Matthew Groves Avatar answered Sep 30 '22 06:09

Matthew Groves


You might have to put the Response.Write in the method that subscribes to the EndRequest (or the BeginRequest) event. I'm guessing the Response object hasn't been fully initialized at the Init stage.

like image 38
Richard Nienaber Avatar answered Sep 30 '22 05:09

Richard Nienaber