会话和HttpContext.Current.Session之间的区别

Session和HttpContext.Current.Session对象有什么区别?

这里有点晚,但是我刚刚发现了一些东西。

@Phillipe Leybaert和@CSharpAtl都不正确。 HttpApplicationSession属性performance出与属性HttpContext.Current.Session不同的行为。 如果可用的话,它们都会返回对同一个HttpSessionState实例的引用。 它们在当前请求没有可用的HttpSessionState实例时所做的不同。

并不是所有的HttpHandler提供会话状态。 为此, HttpHandler 必须实现标记接口IRequiresSessionStateIReadOnlySessionState一个或两个。

如果没有可用的会话, HttpContext.Current.Session简单地返回null

Session属性的HttpApplication的实现抛出一个HttpException ,消息Session state is not available in this context. 而不是返回一个null引用。

不执行会话的HttpHandler的一些示例是通常静态资源(如图像和CSS文件)的默认处理程序。 在这种情况下(如在global.asax事件处理程序中),对HttpApplicationSession属性的任何引用都将导致引发HttpException。

不用说,意外的HttpException提供了一个WTF? 如果你不期待的话。

HttpApplication类的Session属性就这样实现了(来自Reflector):

 [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public HttpSessionState Session { get { HttpSessionState session = null; if (this._session != null) { session = this._session; } else if (this._context != null) { session = this._context.Session; } if (session == null) { throw new HttpException(SR.GetString("Session_not_available")); } return session; } } 

没有区别。

Page.Session的getter返回上下文会话。

Nothing Session只是指向当前的HttpContext会话。