2014-03-29 70 views

回答

1

Microsoft Docs

SessionID屬性是用來唯一標識與服務器上的會話數據的瀏覽器。 SessionID值由ASP.NET隨機生成,並存儲在瀏覽器中的非到期會話cookie中。 SessionID值隨後會在每個請求發送到ASP.NET應用程序的Cookie中。

因此,當瀏覽器請求需要會話狀態的資源時,如果會話狀態模塊無法找到現有會話ID(無論是在ASP.NET會話cookie中,還是在無Cookie會話的情況下),然後創建一個新的會話ID並返回到會話ID Cookie中。

會話ID用於檢索一組會話狀態值(通常稱爲「會話變量」)。只要會話存在,存儲在這種「變量」中的數據將保持可用。如果會話超時,或者AppDomain重新啓動,或者Cookie變得不可用,則會話「變量」將包含空值。使用會話狀態的代碼必須對此有所準備:

糟糕的代碼:

string user = Session["User"]; 
int length = user.Length; // NullReferenceException if session was expired 

更好的代碼:

string user = Session["User"]; 
if (user == null) { 
    // Do without the user information 
} else { 
    int length = user.Length; // User information is available 
} 
+0

-1:報價是準確的,但你的解釋不匹配報價。 –

+0

瀏覽器建立新連接時。我將編輯... –

+0

不,它與連接無關 –

0

會話狀態數據會在所有數據的表現形式,但對於唱歌的用戶全局共享數據。

Declaration For the session : 
    Session["Key"] = "value"; 

    Session data are stored on the server side. 

    Session state variables are declared when session times out. 
    By default time out for session is 20 minutes. 

例如:

**If you are using session & you are executing the first session web form & suppose you have navigated your page from web-form 1 to web-form 2. 
    Then Session output will be the same. 

    If suppose your o/p for web-form 1 is "2". 
    Then if you have closed the browser & copy and paste the same URL to another browser. 
    then you will notice that o/p will be the same Because In your URL session ID is present. 
    there for it will have the same o/p. 
    This is very Important to understand about session if you want to use the session in your application.** 

    ie session data session state data shared across all the web-form but only fr single user. 

    For every session Unique session ID is generated. 
    But is you have changed your session then new session ID is generated meaning it is only for single user. 
+0

我瞭解,當第一個請求轉到服務器時,爲應用程序創建唯一的SessionID,並且映射爲用戶及其各自會話對象(如果有)的唯一身份。如果對我的理解有任何更正,請告知我。感謝所有迴應。 – Shekar

+0

是的,你是完全正確的,Shekar。但有一點要記住的是,如果你想從一個網頁到另一個網頁的一些數據,那麼這是非常有用的。 –

相關問題