Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use HttpRequestMessage as parameter in web api

When do I use the first action?

public HttpResponseMessage Put(HttpRequestMessage message)
{

}

I nearly always see only this way. Maybe because it easily maps to a restfull url?

public HttpResponseMessage Put(int id)
{

}
like image 868
HelloWorld Avatar asked Dec 22 '13 22:12

HelloWorld


People also ask

What is HttpRequestMessage in Web API?

The HttpRequestMessage parameter is automatically bound so that you can get hold of request information in your controller method if you need to (source). If you don't need to access it, omit it. If you need to pass an id , you will need: public HttpResponseMessage Put(HttpRequestMessage message, int id)

What is HttpRequestMessage?

The HttpRequestMessage class contains headers, the HTTP verb, and potentially data. This class is commonly used by developers who need additional control over HTTP requests. Common examples include the following: To examine the underlying SSL/TLS transport information. To use a less-common HTTP method.

What is the usage of delegatinghandler?

Typically, a series of message handlers are chained together. The first handler receives an HTTP request, does some processing, and gives the request to the next handler. At some point, the response is created and goes back up the chain. This pattern is called a delegating handler.

Can we declare FromBody attribute on multiple parameters?

To reiterate, you are allowed to pass only one value (simple or complex type) as parameter to a Web API controller method when using the [FromBody] attribute. You can pass any number of parameters using the [FromUri] attribute but that is not the ideal solution in our case.


1 Answers

public HttpResponseMessage Put(HttpRequestMessage message)

is equivalent to:

public HttpResponseMessage Put()

The HttpRequestMessage parameter is automatically bound so that you can get hold of request information in your controller method if you need to (source). If you don't need to access it, omit it.

If you need to pass an id, you will need:

public HttpResponseMessage Put(HttpRequestMessage message, int id)
like image 198
adrianbanks Avatar answered Sep 27 '22 19:09

adrianbanks