2009-06-02 50 views

回答

7

沒有區別。

Page.Session的getter返回上下文會話。

1

沒有。 Session只是指向HttpContext.Current.Session

94

這裏有點晚,但這裏是我剛發現的東西。

@Phillipe Leybaert和@CSharpAtl都不正確。 HttpApplicationSession屬性展示不同於屬性HttpContext.Current.Session的行爲。如果有一個可用,他們都會返回對同一個HttpSessionState實例的引用。噹噹前請求沒有可用的HttpSessionState實例時,它們的做法會有所不同。

並非所有HttpHandler都提供會話狀態。爲此,HttpHandler必須實施[一個或兩個?]標記接口IRequiresSessionStateIReadOnlySessionState。如果沒有會話可用,則返回null

HttpApplication的實施Session屬性與消息Session state is not available in this context.引發HttpException而不是返回一個null參考。

HttpHandler沒有實現會話的一些示例是通常靜態資源(如圖像和CSS文件)的默認處理程序。在這種情況下(如在global.asax事件處理程序中)對HttpApplicationSession屬性的任何引用都將導致引發HttpException

不用說,意想不到的HttpException提供了一個WTF ?!如果你不期待它。

HttpApplication類的Session性能,從而實現(從反射鏡):

[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; 
    } 
} 
+6

感謝您付出努力填寫更好的答案。 – nicodemus13 2011-05-26 10:19:59

相關問題