2009-09-07 45 views
11

我试图在ASP.NET MVC 2预览版1应用程序中实现会话每请求模式,并且我已经实现了一个IHttpModule来帮助我执行此操作:ASP.NET MVC的HttpModule没有被调用

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

而且我要把它放到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> 

然而,永远不会写入页面(我正在使用内置的VS Web服务器Cassini)。另外,我尝试在SessionModule中放置断点,但它们永远不会中断。我错过了什么?

回答

14

原来在Views文件夹中有一个Web.config文件,而在根目录中有一个。猜猜哪一个我注册了httpModules?是的,视图文件夹之一。我将它移动到根Web.config,现在它就像一个魅力。

+2

+1,因为那正是我所做的,而这正是促使我仔细检查的原因:/ – 2010-09-18 14:29:51

+3

尚未完成但尚未完成。 +1 – Dave 2010-09-27 10:20:23

1

您可能必须将Response.Write放在订阅EndRequest(或BeginRequest)事件的方法中。我猜Init对象在Init阶段还没有完全初始化。

+0

这没有奏效 - > var app =(HttpApplication)sender; app.Context.Response.Write( “EndRequest!”); – 2009-09-07 22:31:41

1

我的问题是我在Controller Method上有[HandleError],在我的自定义错误处理程序有机会之前捕获错误。