0

所以我有我的global.asax Application_Error()事件设置为处理错误,然后执行Server.Transfer()到自定义错误页面。当抛出异常时,我可以通过Application_Error事件观察代码步骤,然后进入我的自定义错误页面的Page_Load()事件,并且无错地移动所有代码;但是我的错误页面永远不会结束渲染。它只停留在我所在的页面上,看起来没有任何事情发生。为什么我的错误页面没有呈现?下面是我的Application_Error事件的代码以及错误页面的Page_Load事件。错误页面不呈现

protected void Application_Error(object sender, EventArgs e) 
    { 
     Exception ex = HttpContext.Current.Server.GetLastError(); 

     if (ex is HttpUnhandledException && ex.InnerException != null) 
      ex = ex.InnerException; 

     if (ex != null) 
     { 
      Guid errorID = Guid.NewGuid(); 
      log.Error(string.Format("=====WEBSITE ERROR===== Error ID: {0}", errorID), ex); 
      Server.Transfer(string.Format("~/Pages/error.aspx?id={0}", errorID)); 
     } 
    } 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     string errorID = Request.QueryString["id"]; 
     Exception ex = HttpContext.Current.Server.GetLastError(); 
     if (ex is HttpUnhandledException && ex.InnerException != null) 
      ex = ex.InnerException; 

     lblErrorID.Text = errorID; 

     if (ex != null) 
     { 
      lblErrorMessage.Text = ex.Message; 
      lblStackTrace.Text = ex.StackTrace.Replace("\n", "<br/>"); 
      plcStackTrace.Visible = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["displayStackTrace"]); 
     } 
     else 
      plcErrorData.Visible = false; 
    } 

也是其值得一提的,我通过Application_Start事件以及使用RouteConfig类的做自定义路由。这不应该影响这一点,因为我在另一个网站上做了完全相同的事情,它的作用就像一个魅力。

回答

0

处理异常后,您必须清除它。只需调用Server.ClearError()

protected void Page_Load(object sender, EventArgs e) 
{ 
    string errorID = Request.QueryString["id"]; 
    Exception ex = HttpContext.Current.Server.GetLastError(); 
    if (ex is HttpUnhandledException && ex.InnerException != null) 
     ex = ex.InnerException; 

    //This works 
    Server.ClearError(); 

    lblErrorID.Text = errorID; 

    if (ex != null) 
    { 
     lblErrorMessage.Text = ex.Message; 
     lblStackTrace.Text = ex.StackTrace.Replace("\n", "<br/>"); 
     plcStackTrace.Visible = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["displayStackTrace"]); 
    } 
    else 
     plcErrorData.Visible = false; 
} 
+0

没有这实际上没有工作。即使使用Server.ClearError()调用,我仍然看到相同的行为。 –

+0

这很奇怪。我重现它和Server.ClearError()做了伎俩。我可能没有考虑路由。 – StrouMfios