会话和HttpContext.Current.Session的区别 [英] Difference between Session and HttpContext.Current.Session

查看:386
本文介绍了会话和HttpContext.Current.Session的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是会话之间的区别HttpContext.Current.Session对象?

What is the difference between Session and HttpContext.Current.Session object?

推荐答案

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

A little late here, but here's something I just discovered.

@Phillipe Leybaert和@CSharpAtl都是不正确。 的HttpApplication 会话属性表现出不同的行为确实该财产 HttpContext.Current的。会议。他们都将返回到相同的 HttpSessionState 实例的引用如果有一个可用。他们的时候没有可用于当前请求没有HttpSessionState实例他们做什么区别。

@Phillipe Leybaert and @CSharpAtl are both incorrect. HttpApplication's Session property exhibits different behaviour than does that of the property HttpContext.Current.Session. They will both return a reference to the same HttpSessionState instance if one is available. They differ in what they do when there is no instance of HttpSessionState available for the current request.

不是所有的的HttpHandler 取值提供会话状态。要做到这一点,在的HttpHandler 必须实施[一个或两个?]标记接口 IRequiresSessionState IReadOnlySessionState

Not all HttpHandlers provide session state. To do so, the HttpHandler must implement [one or both?] the marker interfaces IRequiresSessionState or IReadOnlySessionState.

HttpContext.Current.Session 只返回如果没有可用的会话。

HttpContext.Current.Session simply returns null if there is no session available.

的HttpApplication 的实施Session属性,将引发 HttpException 的消息会话状态不可用在这方面。,而不是返回引用。

The HttpApplication's implementation of the Session property throws an HttpException with the message Session state is not available in this context. rather than return a null reference.

这不实现会话HttpHandlers的一些例子是默认的处理程序通常静态资源,如图像和CSS文件。凡提述在这种情况下的的HttpApplication 会话属性(如的Global.asax 事件处理程序)将导致抛出一个HttpException。

Some examples of HttpHandlers that do not implement session are the the default handlers for normally static resources, such as image and CSS files. Any reference to the HttpApplication's Session property in such cases (as in global.asax event handlers) will result an HttpException being thrown.

不用说了,意外HttpException提供了WTF?此刻,如果你不希望它。

Needless to say, the unexpected HttpException provides a WTF?! moment if you're not expecting it.

的HttpApplication 类的<​​code>会话属性(从反射镜),从而实现

The Session property of the HttpApplication class is implemented thus (from 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;
  }
}

这篇关于会话和HttpContext.Current.Session的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆