C#程序中session的基本设置示例及清除session的方法
session的基本设置:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Web;
usingSystem.Web.SessionState;
namespaceOAFrameWork
{
publicclassCSession
{
publicstaticobjectGet(stringKey)
{
returnHttpContext.Current.Session[Key];
}
publicstaticstringGetString(stringKey)
{
objectobj=HttpContext.Current.Session[Key];
if(obj==null)return"";
elsereturnobj.ToString();
}
publicstaticobjectGet(stringKey,objectDefaultValue)
{
if(HttpContext.Current.Session[Key]==null)
returnDefaultValue;
else
returnHttpContext.Current.Session[Key];
}
publicstaticobjectGet(stringKey,objectDefaultValue,BooleanCanAdd)
{
if(HttpContext.Current.Session[Key]==null)
{
if(CanAdd==true)
HttpContext.Current.Session.Add(Key,DefaultValue);
returnDefaultValue;
}
else
returnHttpContext.Current.Session[Key];
}
publicstaticBooleanSet(stringKey,objectValue)
{
try
{
if(Value==null&&HttpContext.Current.Session[Key]!=null)
{
HttpContext.Current.Session.Remove(Key);
}
elseif(HttpContext.Current.Session[Key]==null)
{
HttpContext.Current.Session.Add(Key,Value);
}
else
{
HttpContext.Current.Session[Key]=Value;
}
returntrue;
}
catch(Exceptionex)
{
CMsgBox.Show(ex.Message);
returnfalse;
}
}
}
}
清除Session:
Session.Abandon();//清除全部Session
//清除某个Session
Session["UserName"]=null;
Session.Remove("UserName");热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下