using System.Web; namespace Ant.Service.Common { /// /// Session 操作类 /// 1、GetSession(string name)根据session名获取session对象 /// 2、SetSession(string name, object val)设置session /// public class SessionHelper { /// /// 根据session名获取session对象 /// /// /// public static object GetSession(string name) { return HttpContext.Current.Session[name]; } /// /// 设置session /// /// session 名 /// session 值 public static void SetSession(string name, object val) { HttpContext.Current.Session.Remove(name); HttpContext.Current.Session.Add(name, val); } /// /// 添加Session,调动有效期为20分钟 /// /// Session对象名称 /// Session值 public static void Add(string strSessionName, string strValue) { HttpContext.Current.Session[strSessionName] = strValue; HttpContext.Current.Session.Timeout = 20; } /// /// 添加Session,调动有效期为20分钟 /// /// Session对象名称 /// Session值数组 public static void Adds(string strSessionName, string[] strValues) { HttpContext.Current.Session[strSessionName] = strValues; HttpContext.Current.Session.Timeout = 20; } /// /// 添加Session /// /// Session对象名称 /// Session值 /// 调动有效期(分钟) public static void Add(string strSessionName, string strValue, int iExpires) { HttpContext.Current.Session[strSessionName] = strValue; HttpContext.Current.Session.Timeout = iExpires; } /// /// 添加Session /// /// Session对象名称 /// Session值数组 /// 调动有效期(分钟) public static void Adds(string strSessionName, string[] strValues, int iExpires) { HttpContext.Current.Session[strSessionName] = strValues; HttpContext.Current.Session.Timeout = iExpires; } /// /// 读取某个Session对象值 /// /// Session对象名称 /// Session对象值 public static string Get(string strSessionName) { if (HttpContext.Current.Session[strSessionName] == null) { return null; } else { return HttpContext.Current.Session[strSessionName].ToString(); } } /// /// 读取某个Session对象值数组 /// /// Session对象名称 /// Session对象值数组 public static string[] Gets(string strSessionName) { if (HttpContext.Current.Session[strSessionName] == null) { return null; } else { return (string[])HttpContext.Current.Session[strSessionName]; } } /// /// 删除某个Session对象 /// /// Session对象名称 public static void Del(string strSessionName) { HttpContext.Current.Session[strSessionName] = null; } /// /// 移除Session /// public static void Remove(string sessionname) { if (HttpContext.Current.Session[sessionname] != null) { HttpContext.Current.Session.Remove(sessionname); HttpContext.Current.Session[sessionname] = null; } } } }