WebService.Session WebService.Session WebService.Session WebService.Session Property


获取当前请求的 HttpSessionState 实例。Gets the HttpSessionState instance for the current request.

 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


表示当前会话的 ASP.NET 会话状态对象的 HttpSessionStateAn HttpSessionState representing the ASP.NET session state object for the current session.


下面的示例使用会话状态来确定特定会话访问 XML Web service 方法SessionHitCounter的次数。The example below uses session state to determine how many times a particular session accesses the XML Web service method SessionHitCounter. 在此示例中, EnableSessionWebMethodAttribute属性设置为true , 以获取对会话状态的访问。In this example, the EnableSession property of the WebMethodAttribute is set to true in order to gain access to session state.

<%@ WebService Language="C#" Class="Util" %>
 using System.Web.Services;
 public class Util: WebService {
   [ WebMethod(Description="Per session Hit Counter",EnableSession=true)]
    public int SessionHitCounter() {
       if (Session["HitCounter"] == null) {
          Session["HitCounter"] = 1;
       else {
          Session["HitCounter"] = ((int) Session["HitCounter"]) + 1;
       return ((int) Session["HitCounter"]);
<%@ WebService Language="VB" Class="Util" %>
Imports System.Web.Services

Public Class Util
    Inherits WebService
    <WebMethod(Description := "Per session Hit Counter", _
        EnableSession := True)> _
    Public Function SessionHitCounter() As Integer
        If Session("HitCounter") Is Nothing Then
            Session("HitCounter") = 1
            Session("HitCounter") = CInt(Session("HitCounter")) + 1
        End If
        Return CInt(Session("HitCounter"))
    End Function
End Class