在Web.config中:
<customErrors mode ="On" defaultRedirect="~/ErrorCustom.aspx" />
customErrors mode ="On" 服务器和客户端都显示自定义错误页面
customErrors mode ="RemoteOnly" 服务器使用标准的错误页面,客户端显示自定义错误页面
在Global.asax中:
protected void Application_Error(object sender, EventArgs e)
{
if (Context != null && Context.IsCustomErrorEnabled)
{
Server.Transfer("ErrorCustom.ASPX", false);
}
}
在ErrorCustion.ASPX中:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Exception ex = Server.GetLastError().GetBaseException();
Label1.Text = ex.Message;
Server.ClearError();
}
}