Nullref on Session.Current in App_Code file
I have a class Session.cs in the App_Code directory that needs to extract some values from the session. I define the session with this:
System.Web.SessionState.HttpSessionState session = HttpContext.Current.Session;
but when I run the page I get a Null Reference Exception on session.
The class (Session.cs) gets called from 开发者_Python百科another .cs class. That gets in turn called by an aspx page.
Could it be that the session doesn't exist in the current part of the page cycle? The calling class gets called from page_load so it should exist.
How can I make sure that I reference the current session correctly? Thanks for reading.
I "solved" this by not using HttpContext and instead setting up a FakeSession class that I would pass to all classes that would need to know sessionID and values from the session. I stored the values I thought I would need as properties in the class.. nothing fancy.
精彩评论