Session_End事件中的Session变量的asp.net值

v s*_*v s 8 asp.net session events session-variables

如果我将值存储在会话变量中

    Session["Int"] = 100;
Run Code Online (Sandbox Code Playgroud)

它在Session_End事件中会是什么?它会是null还是100?

void Session_End(object sender, EventArgs e)
{
      object objInt = Session["Int"];          // Null or 100 ?
}
Run Code Online (Sandbox Code Playgroud)

意思是,Session_End会在会话中或之前处理所有内容后触发吗?

Tim*_*ter 12

这是100.

要自己测试它,只需将ASP.NET应用程序文件添加global.asax到项目中并处理Session_Start结束Session-End事件:

void Session_Start(object sender, EventArgs e)
{
   Session["Int"] = 100;          // 100
}

void Session_End(object sender, EventArgs e)
{
    object objInt = Session["Int"];  // it is still 100 here
}
Run Code Online (Sandbox Code Playgroud)

您可以通过Session.Abandon()(或过期时)结束会话.

protected void Page_Load(object sender, EventArgs e)
{
    Session.Abandon();  // after this Session.End is called
}
Run Code Online (Sandbox Code Playgroud)