asp教程.net中的session的定义和取消有时是分散的,工作组中的每个人定义session的时候不一样,并且名称有随意性,所以做了一个session的统一管理,便于session的规范化。
//interface for session
public interface isession {
string tostring();
}
2. session 类
// managerinfo 是model中的一个类,直接继承
// 将对象放入session中时,该对象必须是可序列化的
[serializable]
public class loginsession : managerinfo , isession
{
public loginsession(): base()
{
sessionid = "";
}public string sessionid { get; set; }
public override int gethashcode()
{
return sessionid.gethashcode();
}public override string tostring()
{
if (!string.isnullorempty(sessionid))
return sessionid;
else
return "";
}
}
session赋值
loginsession currentmanager = new loginsession();
currentmanager.username="admin";
currentmanager.permission="all";
currentmanager.sessionid = httpcontext.current.session.sessionid;
httpcontext.current.session[apps教程etting.globalsessionname] = currentmanager;
httpcontext.current.session.timeout = 200;
取得session的值
public static t getglobalsessionvalue
(string _propertyname)
{
return getsessionvalue(common.const.appsetting.globalsessionname, _propertyname);
}public static t getsessionvalue
(string _sessionname , string _propertyname)
{
t retval = default(t);
string propertyname = _propertyname.tolower();if (convert.tostring(httpcontext.current.session[_sessionname]) != "")
{
object sessionobject = (object)httpcontext.current.session[_sessionname];if (sessionobject is isession)
{
propertyinfo[] propertyinfos = sessionobject.gettype().getproperties();foreach (var pi in propertyinfos)
{
string refname = pi.name.tolower();
if (propertyname == refname)
{
retval = (t)pi.getvalue(sessionobject, null);
break;
}
}
}
}return retval;
}
在程序中可以这样取得session中某一项的值
string _tmpstr = utilities.getglobalsessionvalue("username");
int _tmpint = utilities.getglobalsessionvalue("pagesize");
model.manager = utilities.getglobalsessionvalue("managerdetail");