繁体   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