如何在asp.net中的preinit函数中访问Session?

void Page_PreInit(Object sender, EventArgs e) { HttpCookie userInfo; userInfo = Request.Cookies["userInfo"]; Session["EmpID"] = userInfo["EmpID"]; Session["GroupID"] = userInfo["GroupID"]; Session["DeptID"] = userInfo["DeptID"]; Session["SecID"] = userInfo["SecID"]; if (Session["GroupID"] =="1") { this.MasterPageFile = "master/hr_dept.Master"; } else if (Session["GroupID"] == "2") { this.MasterPageFile = "master/hr_dept.Master"; } else if (Session["GroupID"] == "3") { this.MasterPageFile = "master/hod_dept.Master"; } else if (Session["GroupID"] == "4") { this.MasterPageFile = "master/default2_dept.Master"; } else { this.MasterPageFile = "master/site.Master"; } } 

我想检查我的会话值,然后根据它加载母版页,我在不同的页面上使用相同的母版页。

允许您的类实现IRequiresSessionState,例如:

 public partial class YOUR_ASPX: System.Web.UI.Page , IRequiresSessionState { // your preinit code } 

这是一个标志接口,这意味着不需要实现任何东西,但允许您访问Session。