Partager via


HttpRequestMessageProperty.Method Propriété

Définition

Obtient ou définit le verbe HTTP pour la requête 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

Valeur de propriété

String

Le verbe HTTP pour la requête HTTP.

Exceptions

La valeur est null.

Exemples

Le code suivant obtient une instance de cette classe à partir du message puis distribue aux différentes méthodes basées sur cette propriété.

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

Remarques

Par défaut, WCF utilise le verbe POST pour les messages HTTP. Le verbe GET est utilisé par WCF pour afficher des informations d’aide lors de l’accès à l’adresse de base d’un ServiceHost. Cela est utile pour vérifier si un service WCF est actif lors de l’utilisation d’un navigateur Web. D’autres méthodes définies par le RFC HTTP sont PUT, DELETE, HEAD, OPTIONS, TRACE et CONNECT. Ces méthodes ont des comportements spéciaux lors de l'interaction avec d'autres services.

S’applique à