HttpRequestMessageProperty.Method Property
Gets or sets the HTTP verb for the HTTP request.
Namespace: System.ServiceModel.Channels
Assembly: System.ServiceModel (in System.ServiceModel.dll)
| Exception | Condition |
|---|---|
| ArgumentNullException | The value is set to null. |
By default, WCF uses the POST verb for HTTP messages. The GET verb is used by WCF to display help information when accessing a ServiceHost's base address. This is useful for checking whether a WCF service is active when using a Web browser. Other methods defined by the HTTP RFC are PUT, DELETE, HEAD, OPTIONS, TRACE, and CONNECT. These methods have special behaviors when interoperating with other services.
The following code gets an instance of this class from the message and then dispatches to different methods based on this property.
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; }
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.