C# asp中的自定义错误

C# asp中的自定义错误,c#,asp.net,custom-error-pages,custom-errors,C#,Asp.net,Custom Error Pages,Custom Errors,我试图做一个错误页面重定向到它每当一个错误发生 这是我的代码: <customErrors defaultRedirect="Error.aspx" mode="On" /> 现在运行良好,我如何在我的错误页面上也获取错误消息 示例:Error-Index Error您需要获取发生的最后一个错误(以编程方式)并在页面中显示它。您可以这样做(在Error.aspx中): 其中,lblError是在页面中定义的标签控件,仅用于显示错误消息 有关更多详细信

我试图做一个错误页面重定向到它每当一个错误发生

这是我的代码:

              <customErrors defaultRedirect="Error.aspx" mode="On" />

现在运行良好,我如何在我的错误页面上也获取错误消息


示例:Error-Index Error

您需要获取发生的最后一个错误(以编程方式)并在页面中显示它。您可以这样做(在Error.aspx中):

其中,
lblError
是在页面中定义的标签控件,仅用于显示错误消息

有关更多详细信息,请参阅。

受保护的覆盖无效OnError(EventArgs e)
protected override void OnError(EventArgs e)
{     
  HttpContext ctx = HttpContext.Current;

  Exception exception = ctx.Server.GetLastError ();

  string errorInfo = 
     "<br>Offending URL: " + ctx.Request.Url.ToString () +
     "<br>Source: " + exception.Source + 
     "<br>Message: " + exception.Message +
     "<br>Stack trace: " + exception.StackTrace;

  ctx.Response.Write (errorInfo);
  ctx.Server.ClearError ();

  base.OnError (e);
}
{ HttpContext ctx=HttpContext.Current; Exception=ctx.Server.GetLastError(); 字符串错误信息= “
有问题的URL:”+ctx.Request.URL.ToString()+
来源:“+exception.Source+ “
消息:”+异常。消息+
堆栈跟踪:“+exception.StackTrace; ctx.Response.Write(errorInfo); ctx.Server.ClearError(); base.OnError(e); }

阅读更多有关

的信息,我认为这是一个类似的问题,可能对您在MSDN中的精彩文章(2004年)以及构建一个非常有用的ASP.NET异常引擎非常有用_
protected override void OnError(EventArgs e)
{     
  HttpContext ctx = HttpContext.Current;

  Exception exception = ctx.Server.GetLastError ();

  string errorInfo = 
     "<br>Offending URL: " + ctx.Request.Url.ToString () +
     "<br>Source: " + exception.Source + 
     "<br>Message: " + exception.Message +
     "<br>Stack trace: " + exception.StackTrace;

  ctx.Response.Write (errorInfo);
  ctx.Server.ClearError ();

  base.OnError (e);
}