Reputation: 4454
I'm working on small web app, and when it comes about controllers / endpoints, I saw on many please that
some people are adding [FromBody]
attribute in method parameters and some not.
I can't figure out what is the point ?
Here is the example:
public async Task<ActionResult> UploadImages([FromBody] ProductImagesRequestDto productImagesRequestDto)
vs
public async Task<ActionResult> UploadImages(ProductImagesRequestDto productImagesRequestDto)
Is this endpoint the same?
What is difference in this two methods definitions if there are any ... ?
Thanks everyone
Cheers
Upvotes: -1
Views: 3339
Reputation: 663
No they're not. When you provide the binder then you are saying to model binder to explicitly where to look and what to look for:
FromRoute
binds values from route dataFromBody
binds values from the request bodyFromQuery
binds values from the query stringFromForm
binds values from form fieldsFromHeader
binds values from headersIf you don't provide any binder, you are at the hands of the model binder's default behaviour. It will search in default available binders to match the action parameters. If it can't match any there'll be model state errors or content type errors, if when it is been said to use FromBody
binder and, say, the post data has been sent with x-www-form-urlencoded
.
Upvotes: 2
Reputation: 1
Parameters may be passed through URI or as part of a request body.
Specifying the FromBody attribute simply ensures that the parameter is passed as part of the request body instead of in the URI.
Depending on the type of parameter being passed (and how sensitive that data is), it may be considered best practice to have all parameters in the request body so that it is not visible in the URI
Upvotes: 0