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");热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短