[英]HttpModule with ASP.NET MVC not being called
我試圖在ASP.NET MVC 2 Preview 1應用程序中實現一個每請求會話模式,並且我實現了一個IHttpModule來幫助我這樣做:
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中放置斷點但它們永遠不會破壞。 我錯過了什么?
原來在Views文件夾中有一個Web.config文件,在根目錄中有一個。 猜猜我在哪個注冊了httpModules? 是的,Views文件夾一個。 我將它移動到根Web.config,現在它就像一個魅力。
您可能必須將Response.Write放在訂閱EndRequest(或BeginRequest)事件的方法中。 我猜測在Init階段尚未完全初始化Response對象。
我的問題是我在Controller方法上有[HandleError],它在我的自定義錯誤處理程序有機會之前捕獲了錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.