ASP.NET: Access Session variable in global.asax

前端 未结 3 721
长发绾君心
长发绾君心 2021-01-18 01:34

I have an ASP.NET application and in the Global.asax \' Application Error Event, I am calling a method to trace/log the error. I want to use the session variable content her

相关标签:
3条回答
  • 2021-01-18 02:20

    I think applicaiton error is specific for the whole application and a session is specific for the user. Maybe you can throw your own Exception where you save the information from the session inside your exception.

    0 讨论(0)
  • 2021-01-18 02:28

    It should work if you do it like this:

    strError = System.Web.HttpContext.Current.Session["trCustomerEmail"]
    

    Because that is what I do myself.

    What exactly do you mean with: Visual Studio is telling that "Session is not available in this context"? Do you get a compiler error or a run-time exception?

    You could try to be more defensive and test if there actually is a current HttpContext and a Session:

    if (HttpContext.Current != null &&
        HttpContext.Current.Session != null) {
      strError = HttpContext.Current.Session["trCustomerEmail"]
    }
    
    0 讨论(0)
  • 2021-01-18 02:33

    You may try this:

    HttpContext context = ((HttpApplication)sender).Context;
    

    then you must use is like this:

    context.Request.QueryString.ToString()
    context.Session["key"] = "fasfaasf";
    

    but if the exception was thrown before the Session object is loaded, it will be null

    0 讨论(0)
提交回复
热议问题