I'm in a C# / ASP.NET project. I'd like to be able to get a handle on the Session object (HttpSessionState) from a static context. Is there there any way to do so?
+2
A:
It sounds like you are looking for:-
var sess = HttpContext.Current.Session;
AnthonyWJones
2009-12-16 10:49:45
+2
A:
Yes, the Current
property on HttpContext
is static, therefore:
System.Web.HttpContext.Current.Session
will return the current session from a static context (but you must be inside an HTTP context, or Current
will be null).
Lck
2009-12-16 10:49:56
+2
A:
Try this:
private static new HttpSessionState Session
{
get { return HttpContext.Current.Session; }
}
then from another static function you can just refer to it as
var myObj = Session[myKey];
just like you would from your regular non-static code.
slugster
2009-12-16 10:50:05
+1 This is a very good idea - thanks!
Shaul
2009-12-16 10:55:42
I'll give you answer credit for this creative idea. :)
Shaul
2009-12-16 10:56:19