我为我的应用程序设置了一个自定义错误页面:
<customErrors mode="On" defaultRedirect="~/errors/GeneralError.aspx"
/>
在Global.asax的Application_Error()中,以下代码用于获取异常详细信息:
Exception ex = Server.GetLastError();
if (ex != null)
{
if (ex.GetBaseException() != null)
ex = ex.GetBaseException();
}
当我到达我的错误页面(~/errors/GeneralError.aspx.cs)时,Server.GetLastError()是空的
有没有办法在Error Page上获得异常详细信息,而不是在Global.asax.cs中?
Vista/IIS7上的ASP.NET 3.5
发布于 2009-04-23 04:27:36
尝试在global.asax.cs的Application_Error()
方法中使用类似于Server.Transfer("~/ErrorPage.aspx");
的内容
然后,在ErrorPage.aspx.cs的Page_Load()
中,您应该可以执行如下操作:Exception exception = Server.GetLastError().GetBaseException();
Server.Transfer()
似乎一直在处理这个异常。
发布于 2011-06-08 04:39:09
虽然这里有几个很好的答案,但我必须指出,在错误页面上显示系统异常消息并不是一种好的做法(我假设您想要这样做)。您可能会无意中将您不希望这样做的内容透露给恶意用户。例如,Sql Server异常消息非常冗长,可以在发生错误时提供数据库的用户名、密码和架构信息。该信息不应显示给最终用户。
发布于 2011-09-29 00:03:17
这是我的解决方案..
在Global.aspx中:
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
//direct user to error page
Server.Transfer("~/ErrorPages/Oops.aspx");
}
在Oops.aspx中:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadError(Server.GetLastError());
}
protected void LoadError(Exception objError)
{
if (objError != null)
{
StringBuilder lasterror = new StringBuilder();
if (objError.Message != null)
{
lasterror.AppendLine("Message:");
lasterror.AppendLine(objError.Message);
lasterror.AppendLine();
}
if (objError.InnerException != null)
{
lasterror.AppendLine("InnerException:");
lasterror.AppendLine(objError.InnerException.ToString());
lasterror.AppendLine();
}
if (objError.Source != null)
{
lasterror.AppendLine("Source:");
lasterror.AppendLine(objError.Source);
lasterror.AppendLine();
}
if (objError.StackTrace != null)
{
lasterror.AppendLine("StackTrace:");
lasterror.AppendLine(objError.StackTrace);
lasterror.AppendLine();
}
ViewState.Add("LastError", lasterror.ToString());
}
}
protected void btnReportError_Click(object sender, EventArgs e)
{
SendEmail();
}
public void SendEmail()
{
try
{
MailMessage msg = new MailMessage("webteam", "webteam");
StringBuilder body = new StringBuilder();
body.AppendLine("An unexcepted error has occurred.");
body.AppendLine();
body.AppendLine(ViewState["LastError"].ToString());
msg.Subject = "Error";
msg.Body = body.ToString();
msg.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient("exchangeserver");
smtp.Send(msg);
}
catch (Exception ex)
{
lblException.Text = ex.Message;
}
}
https://stackoverflow.com/questions/343014
复制相似问题