【问题标题】:Display custom error in error.aspx page in asp.net webforms在 asp.net webforms 的 error.aspx 页面中显示自定义错误
【发布时间】:2018-12-22 07:20:35
【问题描述】:

当发生任何错误时,我想在 error.aspx 页面中显示自定义错误。发生错误时,它会重定向到 error.aspx。但没有显示消息。

我将 web.config 设置为

<customErrors mode="On" defaultRedirect="/error.aspx" redirectMode="ResponseRewrite">
  <error statusCode="404" redirect="/notfound.aspx" /></customErrors>

有什么方法可以在error.aspx 页面中显示错误信息。

【问题讨论】:

    标签: asp.net web-config custom-errors


    【解决方案1】:

    您可以拨打GetLastError() 并显示错误。

    // Get the last error from the server
    var ex = Server.GetLastError();
    
    if (null != ex)
    {
       // get and render here the error, there are a lot more info to see.
       txtLiteral.Text = ex.Message;
    }
    
    // and clear the error from the server
    Server.ClearError();
    

    但我建议将其写到日志中,而不是显示给您的用户。同样在本地没有自定义页面的原因,保持原样以查看错误。

    【讨论】:

    • 好的。但是我应该在哪里写这个代码,在 error.aspx??
    • @Vahid in notfound.aspx 你要求的
    猜你喜欢
    • 2013-06-03
    • 2012-06-29
    • 2016-08-18
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    • 1970-01-01
    相关资源
    最近更新 更多