为什么cs类文件中的会话为空? (网络项目.net 2010)

时间:2013-05-19 09:42:35

标签: asp.net class session session-variables webproject

我在vs 2010中的Web项目中有一个cs类文件。 在课堂上,我阅读和wirte会话变量。 但我的Session属性为null!

我用这个:

 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;
            }

        }

但我的HttpContext.Current.Session为null(没有HttpContext.Current.Session [“x”])

注意网页项目没有App_Code文件夹。

如何在班上使用会话?

1 个答案:

答案 0 :(得分:1)

因为您的属性是静态的,并且Sessions不是静态的。 所以你无法访问它。

有关更多信息,请参阅以下链接: MSDN