How can I get the value of a session variable inside a static method? How can I get the value of a session variable inside a static method? asp.net asp.net

How can I get the value of a session variable inside a static method?


HttpContext.Current.Session["..."]

HttpContext.Current gets you the current ... well, Http Context; from which you can access: Session, Request, Response etc


If you haven't changed thread, you can use HttpContext.Current.Session, as indicated by jwwishart.

HttpContext.Current returns the context associated with the thread. Obviously this means you can't use it if you've started a new thread, for example. You may also need to consider thread agility - ASP.NET requests don't always execute on the same thread for the whole of the request. I believe that the context is propagated appropriately, but it's something to bear in mind.


Try this:

HttpContext.Current.Session["UserName"].ToString();