nai*_*own 111 asp.net web-applications exception custom-error-pages
我为我的应用程序设置了自定义错误页面:
<customErrors mode="On" defaultRedirect="~/errors/GeneralError.aspx"
/>
Run Code Online (Sandbox Code Playgroud)
在Global.asax,Application_Error()中,以下代码用于获取异常详细信息:
Exception ex = Server.GetLastError();
if (ex != null)
{
if (ex.GetBaseException() != null)
ex = ex.GetBaseException();
}
Run Code Online (Sandbox Code Playgroud)
当我到达我的错误页面(〜/ errors/GeneralError.aspx.cs)时,Server.GetLastError()为null
有没有什么办法可以在错误页面上获取异常详细信息,而不是在Global.asax.cs中?
Vista/IIS7上的ASP.NET 3.5
nai*_*own 136
仔细查看我的web.config设置,这篇文章中的一条评论非常有帮助
在asp.net 3.5 sp1中有一个新参数redirectMode
所以我们可以修改customErrors添加这个参数:
<customErrors mode="RemoteOnly" defaultRedirect="~/errors/GeneralError.aspx" redirectMode="ResponseRewrite" />
Run Code Online (Sandbox Code Playgroud)
该ResponseRewrite模式允许我们加载«错误页面»而无需重定向浏览器,因此URL保持不变,对我来说重要的是,异常信息不会丢失.
nai*_*own 37
好的,我发现这篇文章:http: //msdn.microsoft.com/en-us/library/aa479319.aspx
有了这个非常简单的图表:
图http://i.msdn.microsoft.com/Aa479319.customerrors_01(en-us,MSDN.10).gif
本质上,为了获得那些异常细节,我需要将它们自己存储在Global.asax中,以便以后在我的自定义错误页面上进行检索.
似乎最好的方法是在Global.asax中完成大部分工作,自定义错误页面处理有用的内容而不是逻辑.
rlb*_*usa 18
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
}
Run Code Online (Sandbox Code Playgroud)
此外,您需要设置web.config:
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="~/myErrorPage.aspx">
</customErrors>
</system.web>
Run Code Online (Sandbox Code Playgroud)
最后,根据您存储在错误页面中的异常,执行您需要的任何操作:
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 ...
}
Run Code Online (Sandbox Code Playgroud)
小智 6
尝试Server.Transfer("~/ErrorPage.aspx");在Application_Error()global.asax.cs 的方法中使用类似的东西
然后从Page_Load()ErrorPage.aspx.cs中你可以做以下事情:Exception exception = Server.GetLastError().GetBaseException();
Server.Transfer() 似乎保持异常悬而未决.
小智 5
虽然这里有几个好的答案,但我必须指出,在错误页面上显示系统异常消息并不是一个好习惯(这是我假设你想做的事情).您可能会无意中向恶意用户透露您不希望这样做的内容.例如,Sql Server异常消息非常详细,并且可以在发生错误时提供数据库的用户名,密码和架构信息.该信息不应显示给最终用户.
小智 5
这是我的解决方案。
在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");
}
Run Code Online (Sandbox Code Playgroud)
在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;
}
}
Run Code Online (Sandbox Code Playgroud)