【问题标题】:A HttpModule that doesn't work in IIS7在 IIS7 中不起作用的 HttpModule
【发布时间】:2010-02-15 17:10:29
【问题描述】:

我有一个 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 中我可以看到其他模块中的模块。该网站在我上传代码之前和之后的行为似乎相同,这是不应该的。

一个编辑过的代码示例:

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

【问题讨论】:

    标签: c# .net asp.net iis-7 httpmodule


    【解决方案1】:

    也许我错过了什么。但是您是否在system.webServer 部分定义了您的HttpModule?另外,您是否将runAllManagedModulesForAllRequests 设置为true?

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

    【讨论】:

    • 是的,我确实把它放在那里,但忘了在这里写下来。还是谢谢。
    • 只是问显而易见的问题并 100% 确定:您将它放在 System.Web 还是 System.webServer 中?
    • system.webServer,我可以在 IIS7 的模块管理器中看到它,所以它会显示出来。但它没有任何作用。
    【解决方案2】:

    显然,IIS7不接受使用HttpContext.Error,像这样:

    context.Error += RedirectMethod;
    

    相反,我不得不这样做:

    context.AuthenticateRequest += RedirectMethod;
    

    这似乎有效。为什么,我不知道。在将 404 丢到用户面前之前,我只想将重定向作为最后的手段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多