Export (0) Print
Expand All
3 out of 7 rated this helpful - Rate this topic

HttpContext.Session Property

Gets the HttpSessionState object for the current HTTP request.

Namespace:  System.Web
Assembly:  System.Web (in System.Web.dll)
public HttpSessionState Session { get; }

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.

TopicLocation
How to: Read Values from Session StateBuilding ASP .NET Web Applications
How to: Sample Session-State Store ProviderBuilding ASP .NET Web Applications
How to: Display Safe Error MessagesBuilding ASP .NET Web Applications
How to: Save Values in Session StateBuilding ASP .NET Web Applications
How to: Read Values from Session StateBuilding ASP .NET Web Applications
How to: Sample Session-State Store ProviderBuilding ASP .NET Web Applications
How to: Display Safe Error MessagesBuilding ASP .NET Web Applications
How to: Save Values in Session StateBuilding ASP .NET Web Applications

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.

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"]);

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.