/// <summary> /// 信息是否存在 /// </summary> /// <returns></returns> public static bool IsExist <T>(this HttpSessionStateBase session, string Key) { bool ret = false; T userInfo = session.GetValue <T>(Key); if (userInfo != null) { ret = true; } return(ret); }
/// <summary> /// Ensures a specific key to be either already in the ASP.NET MVC session state or to be newly created /// </summary> /// <typeparam name = "T">The generic type to be returned</typeparam> /// <param name = "sessionState">The session state.</param> /// <param name = "key">The session state key.</param> /// <returns>The session state value.</returns> /// <example> /// <code> /// public List<String> StringValues /// { /// get { return this.Session.Ensure<List<String>>("StringValues"); } /// set { this.ViewState.Set("StringValues", value); } /// } /// </code> /// </example> public static T Ensure <T>(this HttpSessionStateBase sessionState, String key) where T : class, new() { var value = sessionState.GetValue <T>(key); if (null == value) { value = new T(); sessionState[key] = value; } return(value); }