Asp.net 如何在Global.asax中获取会话

Asp.net 如何在Global.asax中获取会话,asp.net,Asp.net,我在SessionUserID中有值,我想在global.asax中的函数中访问此id 如果会话过期,我可以在void session\u End(object sender,EventArgs e)中获取该会话。从其他方面看,它显示错误会话状态在此上下文中不可用。 怎么办?试试这个 if (HttpContext.Current != null && HttpContext.Current.Session != null) { var data= HttpConte

我在SessionUserID中有值,我想在global.asax中的函数中访问此id 如果会话过期,我可以在void session\u End(object sender,EventArgs e)中获取该会话。从其他方面看,它显示错误会话状态在此上下文中不可用。

怎么办?

试试这个

if (HttpContext.Current != null &&
    HttpContext.Current.Session != null) {
  var data= HttpContext.Current.Session["sessionvariablename"];
}
链接


尝试使用


你能给我一个示例代码吗。。。我想知道如何在全局函数中获取该值。问题是,当我关闭浏览器时,我没有在函数中获取会话。当你关闭浏览器时,你不会得到任何服务器端事件。你可以用javascript检测浏览器关闭,并向更新会话发送ajax请求。我如何向更新会话发送ajax请求?(我正在使用母版页)对不起。。。HttpContext.Current.Session始终为空,Session_End除外问题是,当我关闭浏览器时,我没有在函数中获取Session的值存储在当前Session中,在结束该会话后,内存将被销毁,因此,如果要在Session_End方法之后访问该值,您必须将其保存在其他位置
protected void Application_AcquireRequestState(object sender, EventArgs e)
{
  //Some code here
  string strYourKey = HttpContext.Current.Session["YourKey"].ToString();
}