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 入住酒店文案祝福语简短