简体   繁体   English

IIS7 ASPX 404错误页面/Server.Transfer和相对路径问题

[英]IIS7 ASPX 404 Error Pages / Server.Transfer and problems with relative paths

I am trying to implement a universal .NET 3.5 ASPX 404 error page that handles both "not found" requests in IIS 7 (classic mode) as well as 404 HttpExceptions thrown in code. 我正在尝试实现一个通用的.NET 3.5 ASPX 404错误页面,该页面可处理IIS 7(经典模式)中的“未找到”请求以及代码中引发的404 HttpExceptions。 I am avoiding customErrors as I want a true 404 return code rather than a redirect. 我避免了customErrors,因为我想要一个真正的404返回代码而不是重定向。

To do this I have added the following in the system.webserver section of web.config: 为此,我在web.config的system.webserver部分中添加了以下内容:

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

I have also added a Server.Transfer call in Global.asax Application_Error to capture exceptions thrown in code. 我还在Global.asax Application_Error中添加了Server.Transfer调用,以捕获代码中引发的异常。

The problem is that the relative paths (such as app_themes as well as any links) are calculated relative to the error page rather than the URL of the original request. 问题在于相对路径(例如app_themes以及任何链接)是相对于错误页面而不是原始请求的URL计算的。

So a request for /virtualdir/fail/fail/fail/fail/fail.html tries to find app_themes at /virtualdir/fail/fail/fail/app_themes. 因此,对/virtualdir/fail/fail/fail/fail/fail.html的请求尝试在/ virtualdir / fail / fail / fail / fail / app_themes中找到app_themes。

I think there might be a solution involving Request.RawUrl but I haven't found it yet. 我认为可能存在涉及Request.RawUrl的解决方案,但我尚未找到它。

I'm sure I can't be the first person to have this problem but my Google Fu has let me down this time. 我敢肯定我不是第一个遇到这个问题的人,但是这次我的Google Fu让我失望了。

EDIT 编辑

I've done some investigation and under different circumstances the faulting URL is either maintained in the Request.URL property (desired) or appended to the error page URL as part of the query string in the format (/virtualdir/errorpages/error404.aspx?404;http://host/virtualdir/fail/fail/fail/fail/fail.html). 我进行了一些调查,在不同的情况下,错误的URL要么保留在Request.URL属性中(期望),要么作为查询字符串的一部分附加到错误页面URL中,格式为(/virtualdir/errorpages/error404.aspx 404; http://host/virtualdir/fail/fail/fail/fail/fail.html)。

On my IIS 6 local box, ASPX errors act in the former manner, on the IIS 7 test box, ASPX pages use the latter method. 在我的IIS 6本地框上,ASPX错误以前一种方式起作用,在IIS 7测试框上,ASPX页使用后一种方法。

This is my solution: 这是我的解决方案:

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.
    }
}

It feels a bit dirty though. 不过感觉有点脏。 I am holding out hope for a declarative solution. 我寄希望于声明式解决方案。

I can't test it out right now, but have you tried using the tilde to make the URLs app-relative? 我目前无法对其进行测试,但是您是否尝试过使用波浪号使URL与应用程序相关? eg: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM