Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the function of [FromBody] Attribute in C#?

Is there a function of [FromBody] attribute? I mean, when I use it for example:

public async Task SetUser([FromBody]User user)

and when I use:

public async Task SetUser(User user)

The server get the same object without problems, so, it's necessary set it, or I can remove it without worries?

Grettings!

like image 257
L Leon Avatar asked Aug 26 '16 22:08

L Leon


People also ask

Why do we use FromBody?

The [FromUri] attribute is prefixed to the parameter to specify that the value should be read from the URI of the request, and the [FromBody] attribute is used to specify that the value should be read from the body of the request.

What is difference between FromQuery and FromBody?

[FromQuery] - Gets values from the query string. [FromRoute] - Gets values from route data. [FromForm] - Gets values from posted form fields. [FromBody] - Gets values from the request body.

Why do we have to specify FromBody and FromUri?

To force Web API to read a simple type from the request body, add the [FromBody] attribute to the parameter. So, to answer your question, the need of the [FromBody] and [FromUri] attributes in Web API is simply to override, if necessary, the default behaviour as described above.

What is the use of ApiController attribute?

The [ApiController] attribute applies inference rules for the default data sources of action parameters. These rules save you from having to identify binding sources manually by applying attributes to the action parameters.


2 Answers

User is a complex type, so by default the server will try to resolve it from the request body. If you had a simple type -- e.g.

public async Task SetUser(string userId)

the server would try to resolve the value via URL binding. You can override that behaviour by specifying

public async Task SetUser([FromBody] string userId)

I usually leave [FromBody] in the signature simply for the sake of readability.

like image 182
Rob Lyndon Avatar answered Nov 12 '22 17:11

Rob Lyndon


There are two ways parameters can be passed to the server - via URI, or as part of the request body.

When data are passed in URI, they become query string - e.g. http://server.com/something?userId=5. This will be handled by the action method with int userId argument.

When data are passed in request body, then you cannot see them in the URI - it would be http://server.com/something, for example. Parameters are then passed as name-value pairs inside the request body.

However, in order to pass anything through body, there must be the body, and GET request normally doesn't have request body (it can have it, technically, but I'm not sure if it's used to pass parameters to GET action methods). You would usually expect arguments to be adorned with the FromBody attribute in POST action methods. Likewise, you would usually expect GET actions to receive arguments through URI, which is more in line with the purpose of the GET method.

You can specify either FromUri or FromBody to control behavior. There is also BindModel attribute which lets you define custom binding.

like image 22
Zoran Horvat Avatar answered Nov 12 '22 19:11

Zoran Horvat