簡體   English   中英

一個在IIS7中不起作用的HttpModule

[英]A HttpModule that doesn't work in IIS7

我有一個HttpModule重定向ASP.NET WebForms應用程序中的某些URL。 它可以在我的機器上使用ASP.NET Development Server。 但是當我用IIS7將它上傳到我們的Win2k8服務器時,它似乎根本沒有反應。 我在system.webServer / modules部分放了<add name="Test.Web" type="Test.Web.Core.HttpModules.RedirectOldUrls, Test.Web" /> ,在inetmgr中我可以看到模塊在其他的。 在我上傳代碼之前和之后,網站似乎表現得一樣,但不應該這樣。

編輯過的代碼示例:

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();
    }

也許我錯過了什么。 但是你在system.webServer部分定義了你的HttpModule嗎? 而且,你是否將runAllManagedModulesForAllRequests設置為true?

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

顯然,IIS7不接受使用HttpContext.Error,如下所示:

context.Error += RedirectMethod;

相反,我必須這樣做:

context.AuthenticateRequest += RedirectMethod;

這似乎有效。 為什么,我不知道。 我只想在將404轉移到用戶面前之前重定向作為最后的手段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM