簡體   English   中英

錯誤頁面未呈現

[英]Error Page Not Rendering

所以我將我的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類的使用來進行自定義路由。 這不應該影響這一點,因為我在另一個網站上做同樣的事情,並且它就像一個魅力。

處理異常后,必須清除它。 只需調用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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM