我要在页面里判断session是否过期,如果在每个页面都加代码,很麻烦,我每个页面里都产生一个公共类的实例,我想在该类的构造函数中进行判断,以前问过别人在公共类里使用Response的方法,我想Session也一样,于是构造函数代码如下:
public DBAccess()
{
Session = HttpContext.Current.Session;
Response = HttpContext.Current.Response;
if(Session["UserRole"] == null)
{
WriteMessage("您已掉线,请重新登陆!",false,false);
WriteJavaScript("top.location.href='../Common/index.aspx';");
Response.End();
}
}
结果在执行的时候出现“未将对象设置到实例”的经典错误,请问高手我该怎么办?
public DBAccess()
{
Session = HttpContext.Current.Session;
Response = HttpContext.Current.Response;
if(Session["UserRole"] == null)
{
WriteMessage("您已掉线,请重新登陆!",false,false);
WriteJavaScript("top.location.href='../Common/index.aspx';");
Response.End();
}
}
结果在执行的时候出现“未将对象设置到实例”的经典错误,请问高手我该怎么办?
Response = HttpContext.Current.Response;
感觉有问题(变量名实关键字)
直接if(HttpContext.Current.Session["UserRole"] == null)试试
这样就好了~!
page.session