Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are FromUri and FromQuery the same?

Tags:

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?

like image 835
daniel Avatar asked Dec 26 '17 09:12

daniel


People also ask

What is the difference between FromUri and 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.

Why do we use FromBody?

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).

What is FormBody?

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.

What is FromBody ASP NET core?

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.


Video Answer


1 Answers

[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.

like image 107
twinmind Avatar answered Sep 26 '22 07:09

twinmind