views:

38

answers:

4

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
+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
+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
+1 This is a very good idea - thanks!
Shaul
I'll give you answer credit for this creative idea. :)
Shaul
+1  A: 

HttpContext.Current.Session

ArsenMkrt