HttpContext.Session Property

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets the HttpSessionState object for the current HTTP request.

Namespace:   System.Web
Assembly:  System.Web (in System.Web.dll)

Public ReadOnly Property Session As HttpSessionState

Property Value

Type: System.Web.SessionState.HttpSessionState

The HttpSessionState object for the current HTTP request.

The 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. 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.

The following examples show how to save values in session state and how to read values from session state.

These examples require:

  • An ASP.NET application that has session state enabled.

  • A Web Forms page class that has access to the Page.Session property, or any class that has access to the HttpContext.Current property.

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)

.NET Framework
Available since 1.1
Return to top
Show: