HttpContext.Session HttpContext.Session HttpContext.Session HttpContext.Session Property

定義

取得目前 HTTP 要求的 HttpSessionState 物件。Gets the HttpSessionState object for the current HTTP request.

public:
 property System::Web::SessionState::HttpSessionState ^ Session { System::Web::SessionState::HttpSessionState ^ get(); };
public System.Web.SessionState.HttpSessionState Session { get; }
member this.Session : System.Web.SessionState.HttpSessionState
Public ReadOnly Property Session As HttpSessionState

屬性值

目前 HTTP 要求的 HttpSessionState 物件。The HttpSessionState object for the current HTTP request.

範例

下列範例示範如何儲存會話狀態中的值, 以及如何從會話狀態讀取值。The following examples show how to save values in session state and how to read values from session state.

這些範例需要:These examples require:

  • 已啟用會話狀態的 ASP.NET 應用程式。An ASP.NET application that has session state enabled.

  • 具有Page.Session屬性存取權的 Web Forms 頁面類別, 或可存取HttpContext.Current屬性的任何類別。A Web Forms page class that has access to the Page.Session property, or any class that has access to the HttpContext.Current property.

string firstName = "Jeff";  
string lastName = "Smith";  
string city = "Seattle";  

// Save to session state in a Web Forms page class.  
Session["FirstName"] = firstName;  
Session["LastName"] = lastName;  
Session["City"] = city;  

// Read from session state in a Web Forms page class.  
firstName = (string)(Session["FirstName"]);  
lastName = (string)(Session["LastName"]);  
city = (string)(Session["City"]);  

// Outside of Web Forms page class, use HttpContext.Current.  
HttpContext context = HttpContext.Current;  
context.Session["FirstName"] = firstName;  
firstName = (string)(context.Session["FirstName"]);  
Dim firstName As String = "Jeff"  
Dim lastName As String = "Smith"  
Dim city As String = "Seattle"  

' Save to session state in a Web Forms page class.  
Session("FirstName") = firstName  
Session("LastName") = lastName  
Session("City") = city  

' Read from session state in a Web Forms page class.  
firstName = DirectCast(Session("FirstName"), String)  
lastName = DirectCast(Session("LastName"), String)  
city = DirectCast(Session("City"), String)  

' Outside of Web Forms page class, use HttpContext.Current.  
Dim context As HttpContext = HttpContext.Current  
context.Session("FirstName") = firstName  
firstName = DirectCast(context.Session("FirstName"), String)  

備註

屬性可讓您以程式設計方式存取HttpSessionState類別的屬性和方法。 SessionThe Session property provides programmatic access to the properties and methods of the HttpSessionState class.

若要使用會話狀態, 您必須啟用它。In order to use session state you have to enable it. 如需如何啟用會話狀態的詳細資訊, 請參閱在ASP.NET 會話狀態中設定會話狀態總覽。For information about how to enable session state, see Configuring Session State in ASP.NET Session State Overview.

如需如何在會話狀態中儲存值的相關資訊, 請參閱如何:儲存會話狀態中的值。For information about how to save values in session state, see How to: Save Values in Session State. 如需如何從會話狀態讀取值的相關資訊, 請參閱如何:從會話狀態讀取值。For information about how to read values from session state, see How to: Read Values from Session State.

適用於