使用实例:
private User u = new User();
private string urlPath = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
#region 基础数据
u = new User();
u.UserName = "郑德才";
u.UserAge = "27";
u.UserSex = "男";
u.UserSite = "http://www.zhengdecai.com";
urlPath = Server.MapPath("~/file/");
#endregion
sessionInfo();
}
}
/// <summary>
/// Session测试
/// </summary>
private void sessionInfo()
{
SessionHelper.SetSession("userName", "郑德才");
SessionHelper.AddSession("userAge", "27");
SessionHelper.AddSession("userSex", "男", 5);
SessionHelper.AddSession("userSite", "http://www.zhengdecai.com", 5);
string[] s = new string[4] { "郑德才", "27", "男", "http://www.zhengdecai.com" };
SessionHelper.AddsSession("s", s);
SessionHelper.SetSession("u", u);
SessionHelper.GetSession("userName");
User user = SessionHelper.GetSession<User>("u");
string[] userList = SessionHelper.GetsSession("s");
string cStr = SessionHelper.GetSession("userName") + "-" + SessionHelper.GetSession("userAge") + "-" + SessionHelper.GetSession("userSex") + "-" + SessionHelper.GetSession("userSite");
Response.Write("存在Session:" + cStr + "<br />");
SessionHelper.RemoveSession("userName");
SessionHelper.DeleteSession("userAge");
cStr = SessionHelper.GetSession("userName") + "-" + SessionHelper.GetSession("userAge") + "-" + SessionHelper.GetSession("userSex") + "-" + SessionHelper.GetSession("userSite");
Response.Write("存在Session:" + cStr + "<br />");
/*SessionHelper.RemoveAllSession();
cStr = SessionHelper.GetSession("userName") + "-" + SessionHelper.GetSession("userAge") + "-" + SessionHelper.GetSession("userSex") + "-" + SessionHelper.GetSession("userSite");
Response.Write("存在Session:" + cStr + "<br />");*/
SessionHelper.ClearAllSession();
cStr = SessionHelper.GetSession("userName") + "-" + SessionHelper.GetSession("userAge") + "-" + SessionHelper.GetSession("userSex") + "-" + SessionHelper.GetSession("userSite");
Response.Write("存在Session:" + cStr + "<br />");
}类库信息:
/// <summary>
/// Session 通用操作类
/// 1、GetSession(string strSessionName),读取某个Session对象值,转换为字符串
/// 2、GetsSession(string strSessionName),读取某个Session对象值数组,转换为字符串数组
/// 3、GetSession<T>(string key),获取Session的T对象
/// 4、ClearAllSession(),清除所有Session对象
/// 5、RemoveSession(string strSessionName),删除一个指定的Session对象
/// 6、RemoveAllSession(),删除所有Session对象
/// 7、DeleteSession(string strSessionName),删除某个Session对象,值设置为null
/// 8、SetSession(string strSessionName, object val),从新设置Session对象值
/// 9、AddSession(string strSessionName, string strValue),设置Session值,调动有效期为20分钟
/// 10、AddsSession(string strSessionName, string[] strValues),设置Session值组,调动有效期为20分钟
/// 11、AddSession(string strSessionName, string strValue, int iExpires),设置Session值,调动有效期为iExpires分钟
/// 12、AddsSession(string strSessionName, string[] strValues, int iExpires),设置Session值组,调动有效期为iExpires分钟
/// </summary>
public class SessionHelper
{
#region 获取Session信息
/// <summary>
/// 读取某个Session对象值,转换为字符串
/// </summary>
/// <param name="strSessionName">Session对象名</param>
/// <returns>Session对象值</returns>
public static object GetSession(string strSessionName)
{
if (HttpContext.Current.Session[strSessionName] == null)
{
return null;
}
else
{
return HttpContext.Current.Session[strSessionName];
}
}
/// <summary>
/// 读取某个Session对象值数组,转换为字符串数组
/// </summary>
/// <param name="strSessionName">Session对象名</param>
/// <returns>Session对象值数组</returns>
public static string[] GetsSession(string strSessionName)
{
if (HttpContext.Current.Session[strSessionName] == null)
{
return null;
}
else
{
return (string[])HttpContext.Current.Session[strSessionName];
}
}
/// <summary>
/// 获取Session的T对象
/// </summary>
/// <typeparam name="T">T对象</typeparam>
/// <param name="key">缓存Key</param>
/// <returns></returns>
public static T GetSession<T>(string key)
{
object obj = GetSession(key);
return obj == null ? default(T) : (T)obj;
}
#endregion
#region 清除Session信息
/// <summary>
/// 清空所有的Session对象
/// </summary>
/// <returns></returns>
public static void ClearAllSession()
{
HttpContext.Current.Session.Clear();
}
/// <summary>
/// 删除一个指定的Session对象
/// </summary>
/// <param name="name">Session对象名</param>
/// <returns></returns>
public static void RemoveSession(string strSessionName)
{
HttpContext.Current.Session.Remove(strSessionName);
}
/// <summary>
/// 删除所有的Session对象
/// </summary>
/// <returns></returns>
public static void RemoveAllSession()
{
HttpContext.Current.Session.RemoveAll();
}
/// <summary>
/// 删除某个Session对象,值设置为null
/// </summary>
/// <param name="strSessionName">Session对象名</param>
public static void DeleteSession(string strSessionName)
{
HttpContext.Current.Session[strSessionName] = null;
}
#endregion
#region 设置Session值信息
/// <summary>
/// 从新设置Session对象值
/// </summary>
/// <param name="strSessionName">Session对象名</param>
/// <param name="SessionVal">Session值</param>
public static void SetSession(string strSessionName, object SessionVal)
{
HttpContext.Current.Session.Remove(strSessionName);
HttpContext.Current.Session.Add(strSessionName, SessionVal);
}
/// <summary>
/// 设置Session值,调动有效期为20分钟
/// </summary>
/// <param name="strSessionName">Session对象名</param>
/// <param name="strValue">Session值</param>
public static void AddSession(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 AddsSession(string strSessionName, string[] strValues)
{
HttpContext.Current.Session[strSessionName] = strValues;
HttpContext.Current.Session.Timeout = 20;
}
/// <summary>
/// 设置Session值,调动有效期为iExpires分钟
/// </summary>
/// <param name="strSessionName">Session对象名</param>
/// <param name="strValue">Session值</param>
/// <param name="iExpires">调动有效期(分钟)</param>
public static void AddSession(string strSessionName, string strValue, int iExpires)
{
HttpContext.Current.Session[strSessionName] = strValue;
HttpContext.Current.Session.Timeout = iExpires;
}
/// <summary>
/// 设置Session值组,调动有效期为iExpires分钟
/// </summary>
/// <param name="strSessionName">Session对象名</param>
/// <param name="strValues">Session值数组</param>
/// <param name="iExpires">调动有效期(分钟)</param>
public static void AddsSession(string strSessionName, string[] strValues, int iExpires)
{
HttpContext.Current.Session[strSessionName] = strValues;
HttpContext.Current.Session.Timeout = iExpires;
}
#endregion
}以上类库内容来源互联网,站长稍作整理
评论列表: