2

i have a cs class file in a web project in vs 2010. in class,i read and wirte session variables. but my Session property is null!

i use this:

 public static int UserID
        {
            get
            {
                if (HttpContext.Current.Session["UserID"] == null) return 0;
                return int.Parse(HttpContext.Current.Session["UserID"].ToString());
            }
            set
            {
                HttpContext.Current.Session["UserID"] = value;
            }

        }

but my HttpContext.Current.Session is null(no HttpContext.Current.Session["x"])

note web project has no App_Code folder.

how to use session in my class?

1 Answers1

0

Because your property is static and Sessions are not static. So you can not access it.

For more informaton, see links below: MSDN

  • refer to : http://stackoverflow.com/questions/2577183/how-can-i-get-the-value-of-a-session-variable-inside-a-static-method – Hamed elahi Jul 23 '16 at 08:21