在ASP.NET WebForms的error.aspx页中显示自定义错误

时间:2018-12-22 07:20:36

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

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

我将web.config设置为

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

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

1 个答案:

答案 0 :(得分:0)

您可以呼叫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();

但是我建议将其记录下来,而不要显示给用户。同样在本地没有自定义页面的原因,请保留原样以查看错误。