.NET Session操作
2015-07-08 15:27
381 查看
public class SessionHelper { /// <summary> /// 根据session名获取session对象 /// </summary> /// <param name="name"></param> /// <returns></returns> public static object GetSession(string name) { return HttpContext.Current.Session[name]; } /// <summary> /// 设置session /// </summary> /// <param name="name">session 名</param> /// <param name="val">session 值</param> public static void SetSession(string name, object val) { HttpContext.Current.Session.Remove(name); HttpContext.Current.Session.Add(name, val); } /// <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(); } } public static void Set<T>(string strSessionName, T value) where T : class , new() { HttpContext.Current.Session[strSessionName] = value; HttpContext.Current.Session.Timeout = 24 * 60; } /// <summary> /// 读取某个Session对象值 /// </summary> /// <param name="strSessionName">Session对象名称</param> /// <returns>Session对象值</returns> public static T Get<T>(string strSessionName) where T : class , new() { if (HttpContext.Current == null || HttpContext.Current.Session == null || HttpContext.Current.Session[strSessionName] == null) { return null; } else { return HttpContext.Current.Session[strSessionName] as T; } } /// <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; } }
相关文章推荐
- Which Uri Encoding method should i use in C#/.net?
- poj 3278 Catch That Cow
- iOS Block使用
- XMPP个人信息展示
- Highcharts中的可拖动图例
- hdu 4758 - Walk Through Squares(AC自动机+DP)现场赛
- Yii2学习笔记(二):慕课网视频教程笔记
- asp.net 控件 导出 excel
- leetcode Database2 (四)
- 分组统计并计算每组数量sql
- web app变革之rem
- JAVA中Collections.sort()实现List排序的公共方法和自定义方法
- opencv cvResizeWindow cvMoveWindow cvNamedWindow
- 在sql里面加判断条件
- POI操作Excel:cell的背景颜色类型
- Zabbix分布式监控
- SSL协议,安全套接层
- .NET 文件相关的所有操作
- 简述UIPageControl的属性和用法
- Android开发响应检测及内存分析 - 【Heap】