Session 存放物件

新增一個 extension method

public static void SetObject<T>(this ISession session, string key, T value)
        {
            session.SetString(key, JsonConvert.SerializeObject(value));
        }

        public static T GetObject<T>(this ISession session, string key)
        {
            var value = session.GetString(key);
            return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value);
        }

存值 & 取值

HttpContext.Session.SetObject<T>(Key, object);
HttpContext.Session.GetObject<T>(Key);

發佈留言