ASP.NET 2.0:编写错误页面的最佳实践

在asp.net 2.0网站上,编写错误页面的最佳方法是什么.我在以下位置看过以下部分:

> Web.Config

<customErrors mode="RemoteOnly" defaultRedirect="~/Pages/Common/DefaultRedirectErrorPage.aspx">

> Global.asax

void Application_Error(object sender,EventArgs e) 
{ 
}

我没有得到如何以最佳方式使用它们来进行错误处理.

请指导我最好的方法.

解决方法

在我的全局asax中,我总是检查它是什么类型的http错误…

然后转移到web.config中指定的正确错误页面
我喜欢处理通常的嫌疑人,404(丢失的页面)和500(服务器错误)

http状态代码的一些背景是importaint,以了解它们的处理原因:

http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

我的web.config看起来像这样

<customErrors mode="On"  defaultRedirect="~/error.aspx"  >
  <error statusCode="404" redirect="~/lost.aspx"  />
  <error statusCode="500" redirect="~/error.aspx"  />
</customErrors>

我丢失的页面中有逻辑,试图找到他们可能一直在寻找的页面的链接,以及其他一些格式.

我的错误页面有点不同,显示一些错误消息,

所以我处理两者不同.

取决于您是否有安全的网站区域,您可能需要处理401/403?

protected void Application_Error(object sender,EventArgs e)
{
    var context = Context;


    var error = context.Server.GetLastError() as HttpException;
    var statusCode = error.GetHttpCode().ToString();

    // we can still use the web.config custom errors information to
    // decide whether to redirect
    var config = (CustomErrorsSection)WebConfigurationManager.GetSection("system.web/customErrors");
    if (config.Mode == CustomErrorsMode.On ||
        (config.Mode == CustomErrorsMode.RemoteOnly && context.Request.Url.Host != "localhost"))
    {
        // set the response status code
        context.Response.StatusCode = error.GetHttpCode();

        // Server.Transfer to correct ASPX file for error
        if (config.Errors[statusCode] != null)
        {

            HttpContext.Current.Server.Transfer(config.Errors[statusCode].Redirect);
        }
        else
            HttpContext.Current.Server.Transfer(config.DefaultRedirect);
    }
}

我服务器转移的原因是搜索引擎不会混淆,并保持我的网站管理员日志有意义…如果你重定向你返回一个http状态302告诉浏览器转到重定向到…然后这个页面下一页返回状态代码200(ok).

302 – > 200,或甚至302 – > 404有不同的含义,只有404 ……

然后说我的404错误页面我确保我设置了http错误的状态代码:

protected void Page_PreRender(object sender,EventArgs e)
{
    Response.Status = "404 Lost";
    Response.StatusCode = 404;

}

这篇文章对我有帮助,我知道我想做什么,但我喜欢这段代码看web.config设置… http://helephant.com/2009/02/improving-the-way-aspnet-handles-404-requests/

Return the right status code

By default the page handling a 404
error page doesn’t return a 404 status
code to the browser. It displays the
error message that you provided to the
user but doesn’t have any extra
information to flag the page as an
error page.

This is called a soft 404. Soft 404
pages aren’t as good as ones that
return the 404 status code because
returning the 404 status code lets
anything accessing your file that the
page is an error page rather than a
real page of you site. This is mostly
useful for search engines because then
they know they should remove dead
pages from their index so users won’t
follow dead links into your site from
results pages.

Pages that return 404 status codes are
also useful for error detection
because they’ll be recorded in your
server logs so if you have unexpected
404 errors,they’ll be easy to find.
Here’s an example of the 404 error
report in Google Webmaster tools:

EDITS

Is it require to write
server.clearerror() in global.asax?
What does it impact

>不,您可以在错误页面上执行此操作,
不确定影响?如果你做了
转移非,如果你重定向,那里
可能是另一种可能性
请求之间发生错误?
我不知道

Why in web.config we should write error.aspx
twice one with status code 500 and another is
defaultredirect

>我使用2,因为丢失的页面应该
显示/并做一些不同的事情
服务器错误.错误页面显示
用户有一个错误,我们
无法从……和它恢复
可能是我们的错.我留下了
任何其他的默认重定向
错误代码也是如此. 403,401,400(
它们更罕见,但应该是
处理)

Can you also tell me the code of error.aspx and lost.aspx.

>这取决于网站的类型你有.你得到的错误是一样的方式,但你用它做什么取决于您.在我丢失的页面上,我搜索用户可能已经拥有的一些内容寻找.我记录的错误页面错误,所以用户友好的oops页面……你需要弄清楚需要什么.

相关文章

引言 本文从Linux小白的视角, 在CentOS 7.x服务器上搭建一个...
引言: 多线程编程/异步编程非常复杂,有很多概念和工具需要...
一. 宏观概念 ASP.NET Core Middleware是在应用程序处理管道...
背景 在.Net和C#中运行异步代码相当简单,因为我们有时候需要...
HTTP基本认证 在HTTP中,HTTP基本认证(Basic Authenticatio...
1.Linq 执行多列排序 OrderBy的意义是按照指定顺序排序,连续...