/// <summary> /// <para> </para> /// 經常使用工具類——Session操做類 /// <para> ----------------------------------------------------------</para> /// <para> AddSession:添加Session,有效期爲默認</para> /// <para> AddSession:添加Session,並調整有效期爲分鐘或幾年</para> /// <para> GetSession:讀取某個Session對象值</para> /// <para> DelSession:刪除某個Session對象</para> /// </summary> public class AngelSession { #region 添加Session,有效期爲默認 /// <summary> /// 添加Session,有效期爲默認 /// </summary> /// <param name="strSessionName">Session對象名稱</param> /// <param name="strValue">Session值</param> public static void Add(string strSessionName, object objValue) { HttpContext.Current.Session[strSessionName] = objValue; } #endregion #region 添加Session,並調整有效期爲分鐘或幾年 /// <summary> /// 添加Session,並調整有效期爲分鐘或幾年 /// </summary> /// <param name="strSessionName">Session對象名稱</param> /// <param name="strValue">Session值</param> /// <param name="iExpires">分鐘數:大於0則以分鐘數爲有效期,等於0則之後面的年爲有效期</param> /// <param name="iYear">年數:當分鐘數爲0時按年數爲有效期,當分鐘數大於0時此參數隨意設置</param> public static void Set(string strSessionName, object objValue, int iExpires, int iYear) { HttpContext.Current.Session[strSessionName] = objValue; if (iExpires > 0) { HttpContext.Current.Session.Timeout = iExpires; } else if(iYear>0) { HttpContext.Current.Session.Timeout = 60 * 24 * 365 * iYear; } } #endregion #region 讀取某個Session對象值 /// <summary> /// 讀取某個Session對象值 /// </summary> /// <param name="strSessionName">Session對象名稱</param> /// <returns>Session對象值</returns> public static object Get(string strSessionName) { return HttpContext.Current.Session[strSessionName]; } #endregion #region 刪除某個Session對象 /// <summary> /// 刪除某個Session對象 /// </summary> /// <param name="strSessionName">Session對象名稱</param> public static void Remove(string strSessionName) { HttpContext.Current.Session.Remove(strSessionName); } #endregion }
分裝後使用更方便快捷 工具