3 回答
TA贡献1848条经验 获得超10个赞
NailItDown和Victor所说的组合。首选/最简单的方法是使用Global.Asax存储错误,然后重定向到自定义错误页面。
Global.asax:
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
Exception ex = Server.GetLastError();
Application["TheException"] = ex; //store the error for later
Server.ClearError(); //clear the error so we can continue onwards
Response.Redirect("~/myErrorPage.aspx"); //direct user to error page
}
此外,您需要设置web.config:
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="~/myErrorPage.aspx">
</customErrors>
</system.web>
最后,根据您存储在错误页面中的异常,执行您需要的任何操作:
protected void Page_Load(object sender, EventArgs e)
{
// ... do stuff ...
//we caught an exception in our Global.asax, do stuff with it.
Exception caughtException = (Exception)Application["TheException"];
//... do stuff ...
}
- 3 回答
- 0 关注
- 913 浏览
添加回答
举报