HttpContext.Session Property
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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
Property Value
The HttpSessionState object for the current HTTP request.
Examples
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"]);
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)
Remarks
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.
Applies to
.NET
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기