asp.net:会话中保存的单个会话变量与对象

Mar*_*man 3 asp.net session

我们有一个复杂页面的各种相关会话变量,其中包含各种各样的事情.目前,这些会话值是单独的页面属性(Guid,String,Integer等).如果我有一个具有这些属性的可序列化对象并将其保存在会话中,它会更有效吗?

Jam*_*Ide 5

这不太可能是一个问题,但您可以考虑在ViewState中存储特定于页面的值.

我创建了一个名为SessionInfo的静态类,它包含对会话变量的访问,例如:

public static class SessionInfo
{
    private const string AUDITOR_ID_KEY = "AUDITOR_ID_KEY";

    private static T GetSessionObject<T>(string key)
    {
        object obj = HttpContext.Current.Session[key];
        if (obj == null)
        {
            return default(T);
        }
        return (T)obj;
    }

    private static void SetSessionObject<T>(string key, T value)
    {
        if (Equals(value, default(T)))
        {
            HttpContext.Current.Session.Remove(key);
        }
        else
        {
            HttpContext.Current.Session[key] = value;
        }
    }

   public static int AuditorId
   {
       get { return GetSessionObject<int>(AUDITOR_ID_KEY); }
       set { SetSessionObject<int>(AUDITOR_ID_KEY, value); }
   }
}
Run Code Online (Sandbox Code Playgroud)