HttpRequestMessageProperty.Method Property
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets or sets the HTTP verb for the HTTP request.
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
The HTTP verb for the HTTP request.
The value is set to null
.
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;
}
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
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.
Prodotto | Versioni |
---|---|
.NET | Core 1.0, Core 1.1, 8 (package-provided) |
.NET Framework | 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1 |
.NET Standard | 2.0 (package-provided) |
UWP | 10.0 |
Feedback su .NET
.NET è un progetto di open source. Selezionare un collegamento per fornire feedback: