HttpContext.Session 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为当前 HTTP 请求获取 HttpSessionState 对象。
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 对象。
示例
以下示例演示如何在会话状态中保存值,以及如何从会话状态中读取值。
这些示例需要:
启用了会话状态的 ASP.NET 应用程序。
有权访问 Page.Session 属性的 Web 窗体页类,或任何有权访问 属性的 HttpContext.Current 类。
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)
注解
属性 Session 提供对 类的属性和方法的 HttpSessionState 编程访问。
若要使用会话状态,必须启用它。 有关如何启用会话状态的信息,请参阅 在ASP.NET 会话状态中配置会话状态概述。
有关如何在会话状态下保存值的信息,请参阅 如何:在会话状态中保存值。 有关如何从会话状态读取值的信息,请参阅 如何:从会话状态读取值。