Response.redirect does not preserve HttpContext.Current.Items

后端 未结 2 1346
暗喜
暗喜 2020-12-20 08:06

I was learning about the HttpContext and found out that

HttpContext object will be constructed newly for every request given to an ASP.

相关标签:
2条回答
  • 2020-12-20 08:11

    The redirect generates a new HttpContext which is why the items in it are lost - the redirect effectively tells the browser the next URL to request, and when it does it loses the context of the previous request that triggered the redirect.

    The session persists across requests (typically using a sessionID cookie to tie the user to values on the server), and is thus still available.

    0 讨论(0)
  • 2020-12-20 08:15

    I recommend to you to look with dotPeek at System.Web.dll, in particular at property HttpContext.Session that uses in getter HttpContext.Items["AspSession"] and at method SessionStateModule.InitStateStoreItem(...) (called in that getter) wich calls SessionStateUtility.AddHttpSessionStateToContext(...). You can see, that content of HttpSessionState collection is stored between requests in SessionStateStoreProviderBase implemented (InProc, Sql) object, more deeper in one word. Whereas HttpContext.Items hashtable is initialized at first get and dies between requests.

    0 讨论(0)
提交回复
热议问题