您可以在会话中存储错误详细信息并在自定义错误页面中获取它们。
此代码位于Global.asax中:
protected void Application_Error(object sender, EventArgs e)
{
Exception err = Server.GetLastError();
Session.Add("LastError", err);
}
void Session_Start(object sender, EventArgs e)
{
Session["LastError"] = ""; //initialize the session
}
然后在你的错误页面加载:
protected void Page_Load(object sender, EventArgs e)
{
Exception err = Session["LastError"] as Exception;
//Exception err = Server.GetLastError();
if (err != null)
{
err = err.GetBaseException();
lblErrorMsg.Text = err.Message;
lblSource.Text = err.Source;
lblInnerEx.Text = (err.InnerException != null) ? err.InnerException.ToString() : "";
lblStackTrace.Text = err.StackTrace;
Session["LastError"] = null;
}
}