다음을 통해 공유


HttpRequestMessageProperty.Method 속성

정의

HTTP 요청의 HTTP 동사를 가져오거나 설정합니다.

public:
 property System::String ^ Method { System::String ^ get(); void set(System::String ^ value); };
public string Method { get; set; }
member this.Method : string with get, set
Public Property Method As String

속성 값

String

HTTP 요청의 HTTP 동사입니다.

예외

값이 null로 설정된 경우

예제

다음 코드는 메시지에서 이 클래스의 인스턴스를 가져오고 이 속성에 따라 다른 메서드로 디스패치합니다.

public Message ProcessMessage(Message request)
{
    Message response = null;

    //The HTTP Method (e.g. GET) from the incoming HTTP request
    //can be found on the HttpRequestMessageProperty. The MessageProperty
    //is added by the HTTP Transport when the message is received.
    HttpRequestMessageProperty requestProperties =
        (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];

    //Here we dispatch to different internal implementation methods
    //based on the incoming HTTP verb.
    if (requestProperties != null)
    {
        if (String.Equals("GET", requestProperties.Method,
            StringComparison.OrdinalIgnoreCase))
        {
            response = GetCustomer(request);
        }
        else if (String.Equals("POST", requestProperties.Method,
            StringComparison.OrdinalIgnoreCase))
        {
            response = UpdateCustomer(request);
        }
        else if (String.Equals("PUT", requestProperties.Method,
            StringComparison.OrdinalIgnoreCase))
        {
            response = AddCustomer(request);
        }
        else if (String.Equals("DELETE", requestProperties.Method,
            StringComparison.OrdinalIgnoreCase))
        {
            response = DeleteCustomer(request);
        }
        else
        {
            //This service doesn't implement handlers for other HTTP verbs (such as HEAD), so we
            //construct a response message and use the HttpResponseMessageProperty to
            //set the HTTP status code to 405 (Method Not Allowed) which indicates the client 
            //used an HTTP verb not supported by the server.
            response = Message.CreateMessage(MessageVersion.None, String.Empty, String.Empty);

            HttpResponseMessageProperty responseProperty = new HttpResponseMessageProperty();
            responseProperty.StatusCode = HttpStatusCode.MethodNotAllowed;

            response.Properties.Add( HttpResponseMessageProperty.Name, responseProperty );
        }
    }
    else
    {
        throw new InvalidOperationException( "This service requires the HTTP transport" );
    }

    return response;
}
Public Function ProcessMessage(ByVal request As Message) As Message Implements IUniversalContract.ProcessMessage
    Dim response As Message = Nothing

    'The HTTP Method (e.g. GET) from the incoming HTTP request
    'can be found on the HttpRequestMessageProperty. The MessageProperty
    'is added by the HTTP Transport when the message is received.
    Dim requestProperties As HttpRequestMessageProperty = CType(request.Properties(HttpRequestMessageProperty.Name), HttpRequestMessageProperty)

    'Here we dispatch to different internal implementation methods
    'based on the incoming HTTP verb.
    If requestProperties IsNot Nothing Then
        If String.Equals("GET", requestProperties.Method, StringComparison.OrdinalIgnoreCase) Then
            response = GetCustomer(request)
        ElseIf String.Equals("POST", requestProperties.Method, StringComparison.OrdinalIgnoreCase) Then
            response = UpdateCustomer(request)
        ElseIf String.Equals("PUT", requestProperties.Method, StringComparison.OrdinalIgnoreCase) Then
            response = AddCustomer(request)
        ElseIf String.Equals("DELETE", requestProperties.Method, StringComparison.OrdinalIgnoreCase) Then
            response = DeleteCustomer(request)
        Else
            'This service doesn't implement handlers for other HTTP verbs (such as HEAD), so we
            'construct a response message and use the HttpResponseMessageProperty to
            'set the HTTP status code to 405 (Method Not Allowed) which indicates the client 
            'used an HTTP verb not supported by the server.
            response = Message.CreateMessage(MessageVersion.None, String.Empty, String.Empty)

            Dim responseProperty As New HttpResponseMessageProperty()
            responseProperty.StatusCode = HttpStatusCode.MethodNotAllowed

            response.Properties.Add(HttpResponseMessageProperty.Name, responseProperty)
        End If
    Else
        Throw New InvalidOperationException("This service requires the HTTP transport")
    End If

    Return response
End Function

설명

기본적으로 WCF는 HTTP 메시지에 POST 동사를 사용합니다. GET 동사는 WCF에서 ServiceHost의 기본 주소에 액세스할 때 도움말 정보를 표시하는 데 사용됩니다. 웹 브라우저를 사용할 때 WCF 서비스가 활성 상태인지 여부를 확인하는 데 유용합니다. HTTP RFC에서 정의한 다른 방법은 PUT, DELETE, HEAD, OPTIONS, TRACE 및 CONNECT입니다. 이러한 메서드는 다른 서비스와 상호 운용할 때 특별한 동작을 갖습니다.

적용 대상