调动有效期为20分钟 /// /summary /// paramSession对象名称/param /// param

时间:2021-10-19 08:02:07

这个C#类对session操纵进行了再次封装,可以大大简化session的常用操纵,同时这个类可以将session值设置为数组,也可以将值读取为数组列表,如果你有这方面的需要可以使用这个类,扩这本身对这个C#类进行扩展。

using System.Web; namespace DotNet.Utilities { public static class SessionHelper2 { /// <summary> /// 添加Session,更调有效期为20分钟 /// </summary> /// <param>Session东西名称</param> /// <param>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>Session东西名称</param> /// <param>Session值数组</param> public static void Adds(string strSessionName, string[] strValues) { HttpContext.Current.Session[strSessionName] = strValues; HttpContext.Current.Session.Timeout = 20; } /// <summary> /// 添加Session /// </summary> /// <param>Session东西名称</param> /// <param>Session值</param> /// <param>更调有效期(分钟)</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>Session东西名称</param> /// <param>Session值数组</param> /// <param>更调有效期(分钟)</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>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>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>Session东西名称</param> public static void Del(string strSessionName) { HttpContext.Current.Session[strSessionName] = null; } } }