C# Session添加、刪除封裝類


    /// <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
    }

分裝后使用更方便快捷 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM