2010-02-15 29 views
4

我有一個HttpModule在ASP.NET WebForms應用程序中重定向某個URL:s。它可以在我的機器上使用ASP.NET開發服務器。但是當我用IIS7將它上傳到我們的Win2k8服務器時,它似乎沒有任何反應。我已將<add name="Test.Web" type="Test.Web.Core.HttpModules.RedirectOldUrls, Test.Web" />放入system.webServer/modules部分,並且在inetmgr中我可以看到其他模塊。在我上傳代碼之前和之後,該網站似乎都以相同的方式運行,而不應該這樣做。在IIS7中無法使用的HttpModule

經過編輯的代碼示例:

public void Init(HttpApplication context) 
    { 
     context.Error += PageNotFoundHandler; 
    } 

    public static void PageNotFoundHandler(object sender, EventArgs evt) 
    { 
     Exception lastErrorFromServer = HttpContext.Current.Server.GetLastError(); 

     if (lastErrorFromServer != null) 
     { 
      RedirectToNewUrlIfApplicable(); 
     } 
    } 

    private static void RedirectToNewUrlIfApplicable() 
    { 
     string redirectUrl = GetRedirectUrl(); 

     if (!string.IsNullOrEmpty(redirectUrl)) 
     { 
      HttpContext.Current.Response.Status = "301 Moved Permanently"; 
      HttpContext.Current.Response.AddHeader("Location", redirectUrl); 
     } 
    } 

    private static string GetRedirectUrl() 
    { 
     return RedirectableUrls.GetUrl(); 
    } 

回答

0

顯然,IIS7沒有接受使用HttpContext.Error,像這樣:

context.Error += RedirectMethod; 

相反,我不得不這樣做:

context.AuthenticateRequest += RedirectMethod; 

這似乎工作。爲什麼,我不知道。我只想在404被傾倒在用戶面前作爲最後的手段重定向。

3

也許我失去了一些東西。但是您是否在system.webServer部分中定義了HttpModule?還有,你是否設置runAllManagedModulesForAllRequests爲真?

<modules runAllManagedModulesForAllRequests="true"> 
    <add name="You Module Name" type="Your Module Type"/> 
</modules> 
+0

是的,我確實把它放在那裏,但忘了寫在這裏。不管怎麼說,還是要謝謝你。 – 2010-02-15 17:30:13

+0

只是問清楚,並100%肯定: 你把它放在System.Web或System.webServer? – 2010-02-15 18:52:41

+0

system.webServer,我可以在IIS7的模塊管理器中看到它,所以它顯示出來。但它沒有做任何工作。 – 2010-02-15 19:02:27