Asp.net MVC 3.0 中的 HttpContext.Current.Session 很混乱
我正在使用 ASP.net MVC3.0 应用程序,并将当前用户信息保留在当前 HttpContext 的会话中。
据我所知 HttpContext.Current 是针对每个当前请求的。因此,我的会话数据应该在新请求后清除。但是,我可以通过存储 HttpContext.Current 从请求到请求接收当前用户会话数据。我做这个示例是为了测试目的,以了解 MVC 3.0 中的会话管理。
我的问题:当前请求后如何接收会话数据?我真的很感谢你的帮助。
public static UserAccountDto CurrentUser
{
get
{
if (HttpContext.Current == null)
return null;
if (HttpContext.Current.Session[CurrentUserSessionVariable] != null)
return HttpContext.Current.Session[CurrentUserSessionVariable] as UserAccountDto;
return null;
}
private set { HttpContext.Current.Session[CurrentUserSessionVariable] = value; }
}
I'm working with an ASP.net MVC3.0 application and I keep Current User information in the Session of Current HttpContext.
As I know HttpContext.Current is for per current request.Therefore, my Session data should clear after the new request.However, I can receive Current User session data from request to request by storing HttpContext.Current. I did this sample for testing purpose to understand the session management in MVC 3.0.
My question: How I receive session data after current request ? I really appreciate your help.
public static UserAccountDto CurrentUser
{
get
{
if (HttpContext.Current == null)
return null;
if (HttpContext.Current.Session[CurrentUserSessionVariable] != null)
return HttpContext.Current.Session[CurrentUserSessionVariable] as UserAccountDto;
return null;
}
private set { HttpContext.Current.Session[CurrentUserSessionVariable] = value; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
HttpContext.Current
与以下内容不相同:最后一个在每个请求中都不同,第一个包含许多成员,例如 User、Session、Server 等(但是并非全部)情况下,请求后会出现相同的请求。
HttpContext.Current
is not the same as:the last one is different at every request, the first one contains members like User, Session, Server etc that are in many (but not all) cases the same request after request.
您所做的是正确的,您创建的会话变量将可用于创建它的请求之后的所有请求。
HttpContext 是 Web 开发中最大的对象之一,它在幕后做了很多事情。您之所以不会丢失请求之间的会话,是因为服务器会保持它的活动状态。您会惊讶地发现会话在幕后使用缓存的特定部分
what you have done is correct the session variable you have create will be available for all the request following the one that creates it.
The HttpContext is one of the largest object you will find in web development and behind the scene is does lots of stuff. The reason why you don’t lose the session between the requests is because the server will keep it alive. You will be surprised to know that behind the scene the session uses the particular section of the cache