ASP.NET 2.0 : Best Practice for writing Error Page

标签 asp.net error-handling global-asax

在asp.net 2.0 网站中,编写错误页面的最佳方式是什么。我在以下位置看到了以下部分:

  • 网页配置
    <customErrors mode="RemoteOnly" defaultRedirect="~/Pages/Common/DefaultRedirectErrorPage.aspx">
    

  • 全局.asax
    void Application_Error(object sender, EventArgs e) 
    { 
    }
    


  • 我不知道如何以最佳方式使用它们来处理错误。

    请指导我最好的方法。

    最佳答案

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

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

    有关 http 状态代码的一些背景对于了解处理它们的原因很重要:

    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:



    编辑

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

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


  • 这取决于网站的类型
    你有。你得到同样的错误
    方式,但你用它做什么取决于
    你。在我丢失的页面上我搜索
    用户可能已经浏览过的某些内容
    寻找。我记录的错误页面
    错误等用户友好的哎呀
    页面......你需要弄清楚
    需要什么。
  • 关于ASP.NET 2.0 : Best Practice for writing Error Page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1227632/

    相关文章:

    asp.net - 如何在母版页上放置两次占位符?

    c# - 以编程方式关闭请求验证

    design-patterns - 最佳实践错误处理

    c# - 每个进程初始化 System.Web.HttpApplication 多少次

    asp.net - 创建友好 URL 时如何删除无效字符(即如何创建 slug)?

    asp.net - Eval ("this"的正确等价物是什么)

    ios - 核心数据 - Swift - 捕获 "keypath not found in entity"错误

    perl - 为应用程序设计错误消息系统的最具可扩展性/可扩展性的方法是什么?我将使用 Perl。

    asp.net - Global.asax - FxCop 警告冲突 CA1811 与 CA2109

    asp.net - 为什么在调用 Application_Error() 时 Response.StatusCode 设置为 200?