代码之家  ›  专栏  ›  技术社区  ›  Peter Evjan

在iis7中不工作的http模块

  •  4
  • Peter Evjan  · 技术社区  · 15 年前

    我有一个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();
        }
    
    2 回复  |  直到 15 年前
        1
  •  3
  •   m3kh    15 年前

    也许我错过了什么。但是你定义了你的 HttpModule system.webServer 部分?还有,你设置了 runAllManagedModulesForAllRequests 是真的吗?

    <modules runAllManagedModulesForAllRequests="true">
        <add name="You Module Name" type="Your Module Type"/>
    </modules>
    
        2
  •  0
  •   Peter Evjan    15 年前

    显然,iis7不接受使用httpcontext.error,如下所示:

    context.Error += RedirectMethod;
    

    相反,我不得不这样做:

    context.AuthenticateRequest += RedirectMethod;
    

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