【问题标题】:IIS7 ASPX 404 Error Pages / Server.Transfer and problems with relative pathsIIS7 ASPX 404 错误页面/Server.Transfer 和相对路径问题
【发布时间】:2011-06-13 02:22:48
【问题描述】:

我正在尝试实现一个通用的 .NET 3.5 ASPX 404 错误页面,该页面处理 IIS 7(经典模式)中的“未找到”请求以及代码中抛出的 404 HttpExceptions。我正在避免 customErrors,因为我想要一个真正的 404 返回码而不是重定向。

为此,我在 web.config 的 system.webserver 部分添加了以下内容:

<httpErrors>
  <remove statusCode="404" subStatusCode="-1" />
  <error statusCode="404" subStatusCode="-1" path="/virtualdir/errorpages/error404.aspx" responseMode="ExecuteURL" prefixLanguageFilePath="" />
</httpErrors>

我还在 Global.asax Application_Error 中添加了一个 Server.Transfer 调用来捕获代码中抛出的异常。

问题是相对路径(例如 app_themes 以及任何链接)是相对于错误页面而不是原始请求的 URL 计算的。

因此,对 /virtualdir/fail/fail/fail/fail/fail.html 的请求会尝试在 /virtualdir/fail/fail/fail/app_themes 找到 app_themes。

我认为可能有涉及 Request.RawUrl 的解决方案,但我还没有找到。

我确定我不会是第一个遇到这个问题的人,但这次我的 Google Fu 让我失望了。

编辑

我已经进行了一些调查,在不同的情况下,错误的 URL 要么保留在 Request.URL 属性中(需要),要么作为查询字符串的一部分附加到错误页面 URL,格式为 (/virtualdir/errorpages/ error404.aspx?404;http://host/virtualdir/fail/fail/fail/fail/fail.html)。

在我的 IIS 6 本地机器上,ASPX 错误以前一种方式运行,在 IIS 7 测试机上,ASPX 页面使用后一种方式。

【问题讨论】:

    标签: asp.net iis-7 http-status-code-404 relative-path server.transfer


    【解决方案1】:

    这是我的解决方案:

    string queryString = Request.Url.Query;
    if (queryString.StartsWith("?404;"))
    {
        try
        {
            Uri newUri = new Uri(queryString.Replace("?404;", String.Empty));
    
            HttpContext.Current.RewritePath(newUri.PathAndQuery);
        }
        catch (UriFormatException)
        {
            // Do nothing, the query string is malformed.
        }
    }
    

    虽然感觉有点脏。我对声明式解决方案抱有希望。

    【讨论】:

      【解决方案2】:

      我现在无法对其进行测试,但您是否尝试过使用波浪号使 URL 与应用程序相关?例如:

      <error statusCode="404" subStatusCode="-1" path="~/errorpages/error404.aspx" responseMode="ExecuteURL" prefixLanguageFilePath="" />
      

      【讨论】:

      • 问题在于错误页面相对于错误 URL 的处理方式。我有更多信息,我会尽快发布。
      猜你喜欢
      • 2017-02-08
      • 2013-08-31
      • 2016-02-05
      • 2010-10-23
      • 2010-11-19
      • 1970-01-01
      • 2013-05-29
      • 2013-04-25
      • 1970-01-01
      相关资源
      最近更新 更多