1

Possible Duplicate:
Why HttpContext.Current.Session is null in Global.asax?

I am developing a MVC3 project (Razor).
I added a variable to my session in some Controller/Action (different per user).
I want to access this variable in Application_AuthenticateRequest method (global.asax).

This exception happened:

Session state is not available in this context.

Sample Project

Community
  • 1
  • 1
mehr
  • 845
  • 1
  • 9
  • 18

2 Answers2

0

For startes, see https://stackoverflow.com/a/4185982/717732

The whole point is that Session is not available and simply will never be available at this point of time: and by 'time' I mean 'when this event is fired'.

Read the lifecycle for example at devproconnections.com/article/aspnet2/

The Session object is prepared much later: during the AcquireRequestState event. This is the first 'time' when you can access the Session statebag and investigate it.

Community
  • 1
  • 1
quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
0

Thanks quetzalcoatl, This is true.

      try
        {                
            if (Session != null)
            {
                if (Session["mys"] != null)
                {
                    //Error
                    string s = HttpContext.Current.Session["mys"].ToString();
                }
                else
                {
                    Response.Redirect("~/Home/Index");
                    Response.End();
                }
            }
        }
        catch {
            Response.Redirect("~/Home/Index");
            Response.End();
        }
mehr
  • 845
  • 1
  • 9
  • 18