1.需要在 Global.asax页面中的Application_Error事件中添加处理方法
void Application_Error(object sender, EventArgs e)
{
//在出现未处理的错误时运行的代码
HttpContext ctx = HttpContext.Current;
Exception exception = ctx.Server.GetLastError();
string directUrl = "DefaultError.html?error=";
if (exception != null)
{
string errorInfo = "URL:" + ctx.Request.RawUrl.ToString() + "Source:" + exception.Source
+ "Message:<strong>" + exception.Message + "</strong>";
errorInfo = "错误信息为:<strong>" + exception.InnerException.Message + "</strong>";
//if (exception is HttpException)
//{
// HttpException ex = exception as HttpException;
// int httpCode = ex.GetHttpCode();
// errorInfo = "Code:<strong>" + httpCode.ToString() + "</strong>" + errorInfo;
string url=ctx.Request.Url.ToString();
directUrl = url.Substring(0, url.ToString().IndexOf("/Web/") + 5) + directUrl + errorInfo;
directUrl += "&referurl=" + ctx.Request.Url.ToString();
//if (httpCode == 404)
//{
// directUrl = "~/DefaultError.html?error=" + errorInfo;
//}
//if (httpCode == 403 || httpCode == 402 || httpCode == 401)
//{
// directUrl = "~/DefaultError.html?error=" + errorInfo;
//}
//}
ctx.Items.Add("LastError", errorInfo);
ctx.Server.ClearError();
}
try
{
ctx.Response.Redirect(directUrl);
}
catch
{ }
//ctx.Server.Transfer(directUrl);
}
2.在错误页中获取错误信息并显示出来