locked
How to pass exception details from global.asax to custom error page RRS feed

  • Question

  • User-295014554 posted

    Hi,

    I am capturing the exception from global.asax(with out using try...catch block). So these error become unhandled exceptions. I will capture the exception using server.GetlastError() method. Now I want to store it in session and get it access in the custom error page(I need to customise the error display).But i could not able to access the session values in custom error page.

    Can pl suggest me how to get error details in custom error page. Is there anyway to get the exception details without using session(Without using queystring). How to get the timestamp of the exception(with using exception instance(without using datetime.now))?

    Below i am enclosing the code snippet.

     In Global.asax:protected void Application_Error(Object sender, EventArgs e)

    {

    Exception err = Server.GetLastError();

    Session["Message"] = err.Message.ToString();

    Session["ExceptionType"] = err.GetType().ToString();

    Session["Source"] = err.Source.ToString();

    Session["TargetSite"] = err.TargetSite.ToString();

    Session["Timestamp"] = DateTime.Now.ToString();

    if (err is HttpException && err.InnerException != null)

    {

    err = err.InnerException; //Avoid the Unhandled Exception wrapper which is meaningless

    }

    ExceptionManager.Publish(err);

    }

    In Error_Message.aspx page: 

    protected void Page_Load(object sender, System.EventArgs e)

    {

    if (!IsPostBack)

    {

    StateManager.TrkObjLicPlate =
    string.Empty;

    //LogEntry logEntry = GetLog();

    LogEntry logEntry = new LogEntry();

    logEntry.ExceptionType = Session["ExceptionType"].ToString();

    logEntry.Message = Session["Message"].ToString();

    logEntry.Source = Session["Source"].ToString();

    logEntry.TargetSite = Session["TargetSite"].ToString();

    logEntry.TimeStamp = Session["Timestamp"].ToString();

    }

    }

     

    Please help me getting rid of these issues.

     

    Thanks & Regards,

    Sreeram

    Wednesday, April 1, 2009 10:43 AM

Answers

  • User-43769838 posted

     I think if in your global.asax file you do a server.transfer("Error_Message.aspx") then the current request detail is still available in the ErrorMessage.aspx page.  So in Error_Messgae.aspx you will be able to do Server.GetLastError();

    So use Server.Transfer("Error_Message.aspx") rather than Response.Redirect("Error_Message.aspx")

    Thanks

    tatsky

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 1, 2009 12:47 PM
  • User-1734134863 posted

    Hi,

    Based on my experience,  we need to save the exception message in the session state if we want to visit it in the custom error page. In this scenario, we have to initialize the session in the Session_Start event, otherwise we will get a null value. And we don't need to use the response.redirect method to transfer the page to the error page, in general, we achieve it with making some configurations in the web.config file.

    The code is shown below.

    Global.asax

      void Application_Error(object sender, EventArgs e)
        {   
            Exception err = Server.GetLastError();
            Session["error"] = err.InnerException.Message;   
        }
        void Session_Start(object sender, EventArgs e)
        {     
            Session["error"] = "";//initialize the session
        }

    web.config

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

    CustomerPage.aspx.cs

      protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["error"] != null)
            {
                Response.Write(Session["error"].ToString());
            }
        }

     

    I look forward to receiving your test results.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 5, 2009 10:54 PM

All replies

  • User-43769838 posted

     I think if in your global.asax file you do a server.transfer("Error_Message.aspx") then the current request detail is still available in the ErrorMessage.aspx page.  So in Error_Messgae.aspx you will be able to do Server.GetLastError();

    So use Server.Transfer("Error_Message.aspx") rather than Response.Redirect("Error_Message.aspx")

    Thanks

    tatsky

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 1, 2009 12:47 PM
  • User-295014554 posted

    Thanks tatsky.is there any better to handle this issue without using Session variable.Maintaing the session variable is bit memory consume if data is huge.

    And while using session variable i am facing other problem session is getting lost in the next page. i.e From custom error page will call other page to show the detailed exception.Here i couldn't retrive the session variable value.

            protected void lnkDetails_Click(object sender, EventArgs e)
            {
                Server.Transfer("Reports/ExceptionDetail.aspx);
            }

    suggest me some thing...

    Thanks & Regards,

    Sreeram

    Thursday, April 2, 2009 5:32 AM
  • User-1734134863 posted

    Hi,

    Based on my experience,  we need to save the exception message in the session state if we want to visit it in the custom error page. In this scenario, we have to initialize the session in the Session_Start event, otherwise we will get a null value. And we don't need to use the response.redirect method to transfer the page to the error page, in general, we achieve it with making some configurations in the web.config file.

    The code is shown below.

    Global.asax

      void Application_Error(object sender, EventArgs e)
        {   
            Exception err = Server.GetLastError();
            Session["error"] = err.InnerException.Message;   
        }
        void Session_Start(object sender, EventArgs e)
        {     
            Session["error"] = "";//initialize the session
        }

    web.config

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

    CustomerPage.aspx.cs

      protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["error"] != null)
            {
                Response.Write(Session["error"].ToString());
            }
        }

     

    I look forward to receiving your test results.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 5, 2009 10:54 PM
  • User-589644965 posted

     Thx that's exactly what i am tryint go do but on my detailed error page the session contains 0 items....

    Thursday, April 9, 2009 9:38 AM
  • User-589644965 posted

     Thx that's exactly what i am tryint go do but on my detailed error page the session contains 0 items....

    Thursday, April 9, 2009 9:40 AM
  • User1470709927 posted

    Hi Zong-Qing Li,

    I found this post and your reponse is exactly what I am trying to implement, but my "Application_Error" event is throwing an it's own exception and I was hoping that you could help me.

    When the page exception (that I am trying to pass to the error page) gets thrown, The Global.asax throws it's own error when the session object is being set to the InnerException.Message() value.

    It's telling me: "HttpException was unhandled by user code", "Session State is not available in this context"

    Do you have any experience with what might be causing this, there seems to be rather limited information?

    Thanks for your time,

    Jeremy

    HttpException Image

    Monday, April 12, 2010 3:21 PM
  • User2049305227 posted

    Yeah.. I got exactly the same problem, i worked around that by using a suggestion at the top of the thread. Utilizing the Server.Transfer enabled me to get the error data from any type of error, interestingly the server.transfer has another side effect.

    If an error is thrown in the default.aspx page, the page that comes up is your error page, but it's address in the url and server.url.absolutepath is the default.aspx page. This makes it very easy to capture all the important error data.

    Page, message & stacktrace info.

     

    Rob

    Tuesday, September 27, 2011 10:51 AM