I'm familiar with FromBody
and FromRoute
. They seem to be clear.
I've used FromUri
to handle multi-valued parameters mapping to a list or a string[]
.
FromQuery
sounds similar, but is there any difference?
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.
Using [FromBody] When a parameter has [FromBody], Web API uses the Content-Type header to select a formatter. In this example, the content type is "application/json" and the request body is a raw JSON string (not a JSON object).
To change the default parameter binding process use [FormBody] and [FormUri] attributes. Here, the id with an integer type is declared as [FormBody] attribute. Here in this example, WebAPI is going to look for the request in the value parameter and also in the body parameter and process the request accordingly.
In order to bind the JSON correctly in ASP.NET Core, you must modify your action to include the attribute [FromBody] on the parameter. This tells the framework to use the content-type header of the request to decide which of the configured IInputFormatters to use for model binding.
[FromQuery]
attribute handles query parameters, i.e. key-value pairs coming after "?" in URI. [FromRoute]
attribute handles route parameters coming before "?" in URI, i.e. path parameters.
For example, if you configured route "orders/{id}"
, then "id" is your route parameter, and if some actual request is like "orders/123?showHistory=true", then "showHistory" is your query parameter.
[FromUri]
attribute in Web API works like [FromQuery]
in ASP.NET Core MVC.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With