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