我有以下会话变量:Session["UserId"];
如何将此变量保存在类和公共变量中?像这样的东西:
public class UserDC
{
    //public static Session UserId = Session["UserId"] 
}
我只想打电话:UserDC.UserId。
这是你想要的?
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;
        }
    }
}
编辑:
为了在静态属性或静态方法中获取 Session 变量,您必须实际执行以下操作,因为HttpContext.Current它是静态的:
HttpContext.Current.Session
public static string UserId
{
   get
   {
      return (string)Session["UserId"];
   }
}