• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
风拂晚柳
博客园    首页    新随笔    联系   管理    订阅  订阅

C# SessionHelper

public static class SessionHelper
    {
        /// <summary>  
        /// 添加Session,调动有效期为20分钟  
        /// </summary>  
        /// <param name="strSessionName">Session对象名称</param>  
        /// <param name="strValue">Session值</param>  
        public static void Add(string strSessionName, string strValue)
        {
            HttpContext.Current.Session[strSessionName] = strValue;
            HttpContext.Current.Session.Timeout = 20;
        }

        /// <summary>  
        /// 添加Session,调动有效期为20分钟  
        /// </summary>  
        /// <param name="strSessionName">Session对象名称</param>  
        /// <param name="strValues">Session值数组</param>  
        public static void Adds(string strSessionName, string[] strValues)
        {
            HttpContext.Current.Session[strSessionName] = strValues;
            HttpContext.Current.Session.Timeout = 20;
        }

        /// <summary>  
        /// 添加Session  
        /// </summary>  
        /// <param name="strSessionName">Session对象名称</param>  
        /// <param name="strValue">Session值</param>  
        /// <param name="iExpires">调动有效期(分钟)</param>  
        public static void Add(string strSessionName, string strValue, int iExpires)
        {
            HttpContext.Current.Session[strSessionName] = strValue;
            HttpContext.Current.Session.Timeout = iExpires;
        }

        /// <summary>  
        /// 添加Session  
        /// </summary>  
        /// <param name="strSessionName">Session对象名称</param>  
        /// <param name="strValues">Session值数组</param>  
        /// <param name="iExpires">调动有效期(分钟)</param>  
        public static void Adds(string strSessionName, string[] strValues, int iExpires)
        {
            HttpContext.Current.Session[strSessionName] = strValues;
            HttpContext.Current.Session.Timeout = iExpires;
        }

        /// <summary>  
        /// 读取某个Session对象值  
        /// </summary>  
        /// <param name="strSessionName">Session对象名称</param>  
        /// <returns>Session对象值</returns>  
        public static string Get(string strSessionName)
        {
            if (HttpContext.Current.Session[strSessionName] == null)
            {
                return null;
            }
            else
            {
                return HttpContext.Current.Session[strSessionName].ToString();
            }
        }

        /// <summary>  
        /// 读取某个Session对象值数组  
        /// </summary>  
        /// <param name="strSessionName">Session对象名称</param>  
        /// <returns>Session对象值数组</returns>  
        public static string[] Gets(string strSessionName)
        {
            if (HttpContext.Current.Session[strSessionName] == null)
            {
                return null;
            }
            else
            {
                return (string[])HttpContext.Current.Session[strSessionName];
            }
        }

        /// <summary>  
        /// 删除某个Session对象  
        /// </summary>  
        /// <param name="strSessionName">Session对象名称</param>  
        public static void Del(string strSessionName)
        {
            HttpContext.Current.Session[strSessionName] = null;
        }
    }

 

posted @ 2019-03-04 16:40  风拂晚柳  阅读(382)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3