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

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

问题描述

我有一个.aspx页,上设立IIS 7网站我需要检索用户试图访问,以做404页上的一些处理原始URL我的自定义404页。诀窍是,我需要特别处理404的不包含.aspx扩展名(例如: http://example.com/testurl ),不打通路由ASP.NET的自定义错误部分。我可以配置IIS来指向我的自定义404,但在这一点上,我不知道如何让我原来的网址是什么?有谁知道这是否可能?

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.

有需要处理两种情况,一种是在误差来源于由.NET处理一个.aspx或其他页,而另一个是在误差来自一个坏的文件夹(如在你的问题, http://example.com/testurl ),或不被处理.NET文件名(例如,的* .htm)。在IIS 7中,您需要为您的Web应用中的ASP.NET一节中配置的.NET错误页自定义404错误,也是在IIS部分中的错误页面。在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>

注:该redirectMode =ResponseRewrite上面列出的是很重要的,如果你想你的404页面就返回404消息,我不认为它可以通过IIS设置

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属性读取。我用下面的code在我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状态code,所以你需要手动设置。见<一href=\"http://stackoverflow.com/questions/4302566/asp-net-custom-error-page-for-404-returns-302-for-http-status\">this帖子了解更多详情。

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

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