C#操作session的类实例
本文实例讲述了C#操作session的类。分享给大家供大家参考。具体分析如下:
这个C#类对session操作进行了再次封装,可以大大简化session的常用操作,同时这个类可以将session值设置为数组,也可以将值读取为数组列表,如果你有这方面的需要可以使用这个类,扩这自己对这个C#类进行扩展。
usingSystem.Web;
namespaceDotNet.Utilities
{
publicstaticclassSessionHelper2
{
///<summary>
///添加Session,调动有效期为20分钟
///</summary>
///<paramname="strSessionName">Session对象名称</param>
///<paramname="strValue">Session值</param>
publicstaticvoidAdd(stringstrSessionName,stringstrValue)
{
HttpContext.Current.Session[strSessionName]=strValue;
HttpContext.Current.Session.Timeout=20;
}
///<summary>
///添加Session,调动有效期为20分钟
///</summary>
///<paramname="strSessionName">Session对象名称</param>
///<paramname="strValues">Session值数组</param>
publicstaticvoidAdds(stringstrSessionName,string[]strValues)
{
HttpContext.Current.Session[strSessionName]=strValues;
HttpContext.Current.Session.Timeout=20;
}
///<summary>
///添加Session
///</summary>
///<paramname="strSessionName">Session对象名称</param>
///<paramname="strValue">Session值</param>
///<paramname="iExpires">调动有效期(分钟)</param>
publicstaticvoidAdd(stringstrSessionName,stringstrValue,intiExpires)
{
HttpContext.Current.Session[strSessionName]=strValue;
HttpContext.Current.Session.Timeout=iExpires;
}
///<summary>
///添加Session
///</summary>
///<paramname="strSessionName">Session对象名称</param>
///<paramname="strValues">Session值数组</param>
///<paramname="iExpires">调动有效期(分钟)</param>
publicstaticvoidAdds(stringstrSessionName,string[]strValues,intiExpires)
{
HttpContext.Current.Session[strSessionName]=strValues;
HttpContext.Current.Session.Timeout=iExpires;
}
///<summary>
///读取某个Session对象值
///</summary>
///<paramname="strSessionName">Session对象名称</param>
///<returns>Session对象值</returns>
publicstaticstringGet(stringstrSessionName)
{
if(HttpContext.Current.Session[strSessionName]==null)
{
returnnull;
}
else
{
returnHttpContext.Current.Session[strSessionName].ToString();
}
}
///<summary>
///读取某个Session对象值数组
///</summary>
///<paramname="strSessionName">Session对象名称</param>
///<returns>Session对象值数组</returns>
publicstaticstring[]Gets(stringstrSessionName)
{
if(HttpContext.Current.Session[strSessionName]==null)
{
returnnull;
}
else
{
return(string[])HttpContext.Current.Session[strSessionName];
}
}
///<summary>
///删除某个Session对象
///</summary>
///<paramname="strSessionName">Session对象名称</param>
publicstaticvoidDel(stringstrSessionName)
{
HttpContext.Current.Session[strSessionName]=null;
}
}
}
希望本文所述对大家的C#程序设计有所帮助。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下