C# .NET - use Session variables in a class file

Asked By srilatha janakaraj on 01-Dec-05 06:51 PM
Hi,
In default.aspx, I set some session variables.
Session["UserID"] = "100";
I have lot of class files in my project where I need the above UserID value.
I discovered that I cant use the Session variables in a class file directly, as below
string ID = Session["UserID"]; //does not work in class file
How else can I access the session variable frm the classes?
What I would like to do, is write a function that takes a  string parameter, and return a session variable value..
like
public getSessionVarValue(string s)
{
    return Session["s"];
}
how and where can I write this function..?

Session is part of the current HttpContext

Asked By Peter Bromberg on 01-Dec-05 07:05 PM
So your class files will need a reference to the System.Web assembly. Then, you can access curent Session via:
System.Web.HttpContext.Current.Session["yourItem"];
Suneel replied to Peter Bromberg on 30-Dec-10 01:59 AM
Add This Namespace

using System.Web;

and Use this

HttpContext.Current.Session
["SessionValue"]