0
votes

So I have my global.asax Application_Error() event set up to process errors and then do a Server.Transfer() to a custom error page. When an exception is thrown, I can watch the code step through the Application_Error event, then step into the Page_Load() event of my custom error page and move through all of the code with no errors; however my error page never ends up rendering. It just stays on the page I was on and it looks like nothing happened. Why is my error page not rendering? Below is the code for my Application_Error event as well as the Page_Load event of the error page.

    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;
    }

Also its worth noting I'm doing custom routing via the Application_Start event and the use of a RouteConfig class. This shouldn't be affecting this because I do this exact same thing on another website and it works like a charm.

1

1 Answers

0
votes

After handling the exception, you have to clear it. Just call 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;
}