WebService.Context 屬性

定義

取得目前要求的 ASP.NET HttpContext 將 HTTP 伺服器使用的所有 HTTP 特定內容封裝,以便處理 Web 要求。Gets the ASP.NET HttpContext for the current request, which encapsulates all HTTP-specific context used by the HTTP server to process Web requests.

public:
 property System::Web::HttpContext ^ Context { System::Web::HttpContext ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Web.HttpContext Context { get; }
member this.Context : System.Web.HttpContext
Public ReadOnly Property Context As HttpContext

屬性值

目前要求的 ASP.NET HttpContextThe ASP.NET HttpContext for the current request.

屬性

例外狀況

ContextnullContext is null.

範例

下列範例使用Context屬性來取得伺服器上要求的時間。The Example below uses the Context property to obtain the time of the request on the server.

<%@ WebService Language="C#" Class="Util" %>
 
 using System;
 using System.Web.Services;
 
 public class Util: WebService {
   [ WebMethod(Description="Returns the time as stored on the Server",
   EnableSession=false)]
   public string Time() {
      return Context.Timestamp.TimeOfDay.ToString();
   }
 }
<%@ WebService Language="VB" Class="Util" %>
 
Imports System
Imports System.Web.Services

Public Class Util
    Inherits WebService
    
    <WebMethod(Description := "Returns the time as stored on the Server", _
        EnableSession := False)> _
    Public Function Time() As String
        
        Return Context.Timestamp.TimeOfDay.ToString()
    End Function
End Class

備註

XML Web Service 已SoapRpcMethodAttribute套用或HttpContext OneWay Current true屬性的方法, 且屬性設定為, 則無法使用靜態屬性存取其。 SoapDocumentMethodAttributeXML Web service methods that have either the SoapRpcMethodAttribute or SoapDocumentMethodAttribute attribute applied to them with the OneWay property of set to true, do not have access to their HttpContext using the static Current property. 若要存取HttpContext, 請從WebService衍生類別, 並存取Context屬性來執行 XML Web Service 方法。To access the HttpContext, derive the class implementing the XML Web service method from WebService and access the Context property.

適用於

另請參閱