How can I save a session variable in the object?

asp.net, c#, session

Solution

Is this what you are looking for?

public class UserDC
{
    public static string UserId
    {
        get
        {
            if(HttpContext.Current.Session["Test"] != null)
                return HttpContext.Current.Session["Test"].ToString()
            else 
                return "";
        }

        set
        {
            HttpContext.Current.Session["Test"] = value;
        }
    }
}

Edit:

In order to get a Session variable within a static property or static method, you must actually do the following because `HttpContext.Current` is static:

HttpContext.Current.Session

Problem

I have the following Session variable: `Session["UserId"];` How can I save this variable in the class and public variables? Something like this: ``` public class UserDC { //public static Session UserId = Session["UserId"] } ``` I only want to call: `UserDC.UserId`.

Original source