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

Définition

Obtient l'objet HttpSessionState pour la requête HTTP actuelle.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

Valeur de propriété

Objet HttpSessionState pour la requête HTTP actuelle.The HttpSessionState object for the current HTTP request.

Exemples

Les exemples suivants montrent comment enregistrer des valeurs dans l’état de session et comment lire des valeurs à partir de l’état de session.The following examples show how to save values in session state and how to read values from session state.

Ces exemples nécessitent :These examples require:

  • Une application ASP.NET dont l’état de session est activé.An ASP.NET application that has session state enabled.

  • Une classe de page Web Forms qui a accès à Page.Session la propriété, ou toute classe qui a accès à HttpContext.Current la propriété.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)  
  

Remarques

La Session propriété fournit l’accès par programme aux propriétés et aux méthodes HttpSessionState de la classe.The Session property provides programmatic access to the properties and methods of the HttpSessionState class.

Pour pouvoir utiliser l’état de session, vous devez l’activer.In order to use session state you have to enable it. Pour plus d’informations sur l’activation de l’état de session, consultez Configuration de l' État de session dans ASP.net vue d’ensemblede l’état de session.For information about how to enable session state, see Configuring Session State in ASP.NET Session State Overview.

Pour plus d’informations sur l’enregistrement de valeurs dans l’état de session, consultez Procédure: Enregistrer les valeurs dans l'état de session.For information about how to save values in session state, see How to: Save Values in Session State. Pour plus d’informations sur la lecture des valeurs à partir de l’état de session, consultez Procédure: Lire les valeurs à partirde l’état de session.For information about how to read values from session state, see How to: Read Values from Session State.

S’applique à