开发者

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. I am avoiding customErrors as I want a true 404 return code rather than a redirect.

To do this I have added the following in the system.webserver section of web.config:

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

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.

So a request for /virtualdir/fail/fail/fail/fail/fail.html tries to find app_themes at /virtualdir/fail/fail/fail/app_themes.

I think there might be a solution involving Request.RawUrl but I haven't found it yet.

I'm sure I can't be the first person to have this problem but my Google Fu has let me down this time.

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

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.


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? eg:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜