在 IIS7 404 重定向页面中访问原始 URL [英] Accessing original URL in IIS7 404 redirect page

查看:33
本文介绍了在 IIS7 404 重定向页面中访问原始 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 IIS 7 上设置的站点上有一个 .aspx 页面作为我的自定义 404 页面.我需要检索用户尝试访问的原始 URL,以便在 404 页面上进行一些处理.诀窍是我需要专门处理不包含 .aspx 扩展名的 404(例如 http://example.com/testurl),它们不会通过 ASP.NET 的自定义错误部分路由.我可以将 IIS 配置为指向我的自定义 404,但那时我不知道如何获取我的原始 URL?有谁知道这是否可能?

I have an .aspx page as my custom 404 page on a site set up on IIS 7. I need to retrieve the original URL that the user was trying to access in order to do some processing on the 404 page. The trick is that I need to specifically handle 404's that do not contain a .aspx extension (e.g http://example.com/testurl), which do not get routed through ASP.NET's custom errors section. I can configure IIS to point to my custom 404, but at that point I do not know how to get my original URL? Does anyone know if this is possible?

谢谢,

迈克

推荐答案

是的,有可能获取导致 404 错误的 URL,您只需要确保您已正确配置 IIS.

Yes, it is possible to get the URL that causes the 404 error, you just need to make sure you have IIS configured properly.

您需要处理两种情况,一种是错误来自 .aspx 或由 .NET 处理的其他页面,另一种是错误来自错误文件夹(如您的问题,http://example.com/testurl) 或 .NET 未处理的文件名(例如,*.htm).在 IIS 7 中,您需要在 Web 应用程序的ASP.NET"部分的.NET 错误页面"下以及IIS"部分的错误页面"下配置自定义 404 错误.web.config 更改最终看起来像这样:

There are two cases you need to handle, one is where the error comes from an .aspx or other page that is handled by .NET, and the other is where the error comes from a bad folder (as in your question, http://example.com/testurl) or filename (for example, *.htm) that is not handled by .NET. In IIS 7, you'll need to configure a custom 404 error under ".NET Error Pages" in the "ASP.NET" section for your web app, and also under "Error Pages" in the "IIS" section. The web.config changes end up looking something like this:

<system.web>
    <!-- other system.web stuff -->
    <customErrors defaultRedirect="/Error404.aspx" mode="On" redirectMode="ResponseRewrite">
        <error redirect="/Error404.aspx" statusCode="404" />
    </customErrors>
</system.web>
<system.webServer>
    <!-- other system.webServer stuff -->
    <httpErrors errorMode="Custom">
        <remove statusCode="404" subStatusCode="-1" />
        <error statusCode="404" prefixLanguageFilePath="" path="/Error404.aspx" responseMode="ExecuteURL" />
    </httpErrors>
</system.webServer>

注意:如果您希望您的 404 页面实际返回 404 消息并且我认为它不能通过 IIS 设置,那么上面列出的 redirectMode="ResponseRewrite" 很重要.

Note: the redirectMode="ResponseRewrite" listed above is important if you want your 404 pages to actually return 404 messages and I don't think it can be set through IIS.

在我的示例中,我创建了一个名为 Error404.aspx 的页面来处理所有 404 错误.当 .NET 页面(.aspx 等)抛出 404 异常时,可以在 aspxerrorpath 查询字符串变量中找到原始文件名.当常规 htm 或其他页面导致 404 错误时,可以从 Request.RawUrl 属性读取原始路径.我在 Error404.aspx 页面中使用以下代码来处理任一情况:

In my example, I created a page called Error404.aspx to handle all of the 404 errors. When a .NET page (.aspx, etc) throws a 404 exception, the original filename can be found in the aspxerrorpath querystring variable. When a regular htm or other page causes a 404 error, the original path can be read from the Request.RawUrl property. I used the following code in my Error404.aspx page to handle either case:

public partial class Error404 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        OriginalUrl = Request.QueryString["aspxerrorpath"] ?? Request.RawUrl;
        Server.ClearError();
        Response.Status = "404 not found";
        Response.StatusCode = 404;
    }

    public string OriginalUrl { get; private set; }
}

默认404错误页面不会返回404状态码,需要手动设置.请参阅这篇文章更多细节.

By default, the 404 error page will not return a 404 status code, so you need to set it manually. See this post for more detail.

这篇关于在 IIS7 404 重定向页面中访问原始 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆