asp.net values of Session variables in Session_End event

让人想犯罪 __ 提交于 2019-12-01 01:07:47

问题


If I store a value in a session variable

    Session["Int"] = 100;

What it will be in the Session_End event? Will it be null or 100?

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

Meaning, will Session_End fire after disposing everything in the session or just before?


回答1:


It is 100.

To test it yourself simply add the ASP.NET application file global.asax to your project and handle the Session_Start end Session-End events:

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
}

You can end a Session by Session.Abandon() (or when it expires).

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



回答2:


I found that Session["Int"] will be 100. I set the session timeout to just 1 minute and put a break point in that event.



来源:https://stackoverflow.com/questions/12294532/asp-net-values-of-session-variables-in-session-end-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!