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 红包祝福语简短10字
18 周六早晨祝福语简短