【问题标题】:Access Session values from static properties of static class [duplicate]从静态类的静态属性访问会话值 [重复]
【发布时间】:2018-08-18 10:18:03
【问题描述】:

我想使用在另一个引用项目中声明的静态类的静态属性中的Session 值。有些属性是每个应用程序,有些是每个用户:

public class MyClass
{
    public static string var1 { get; set; } //Always static
    public static string var2 { get; set; } //Need to use Session[Var2]
    public static string var3 { get; set; } //Always static
    public static string var4 { get; set; } //Need to use Session[Var4]
}

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    如果我正确理解了您的问题,您希望将所有用户的会话值存储在一个集合中。因此,会话是为每个用户提供的。您可以为每个用户创建一个静态字典并存储会话值。

    public class MyClass
    {
        public static string var1 { get; set; } //Always static
        public static Dictionary<string,string> var2Dict = new Dictionary<string, string>();
        public static string var3 { get; set; } //Always static
        public static Dictionary<string, string> var4Dict = new Dictionary<string, string>();
    }
    

    然后将用户的会话值添加到字典中;

    MyClass.var2Dict.Add(HttpContext.Current.User.Identity.Name, (string)HttpContext.Current.Session["var2"]);
    

    【讨论】:

      【解决方案2】:
      public class MyClass
      {
        public static string var2 
        {
           get { return (string)System.Web.HttpContext.Current.Session["KEY_VAR2"]; }
           set { System.Web.HttpContext.Current.Session["KEY_VAR2"] = value; } 
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-07-31
        • 1970-01-01
        • 2015-08-13
        • 2013-02-13
        • 2018-04-18
        • 1970-01-01
        • 2013-08-17
        • 1970-01-01
        • 2014-12-07
        相关资源
        最近更新 更多