How to pass data to the API

API data may be transmitted in various ways.

Bindingsource Attributes will tell Model of binding engine to find where to bind values.

Bindingsource Attributes:

Body 1. [FromBody] request

2. [FromForm] form of request data Body

Header 3. [FromHeader] request

4. [FromQuery] Query string parameters   

 eg:public async Task<ActionResult<Student>> GetStudent([FromQuery]Guid id){}

Routing data 5. [From Route] in the current request 

 eg:public async Task<ActionResult<Student>> GetStudent([FromRoute]Guid id){}

6. [FromService] Action parameter as a service injected

 

[ApiController]

ASP.NET Core will use the default Complex Object Model Binder, it will be extracted from the data where Value Providers, and Value Providers order is defined.

But we build API is often used [ApiController] This property, in order to better adapt to the API She changed the rules above.

 

[ApiController] after changing the rules

 

1. [FromBody] is usually used to deduce parameters of complex type (such as using a pass json object that when there are several properties)

 

2. [FromForm] Action commonly used to determine the parameters and IFormFilecollection IFormFile type (upload)

 

3. [From Route] used to extrapolate Action parameter name and routing template parameter names consistent.

 

4. [FromQuery] Action used to infer other parameters. (In addition to the above three kinds of other cases).

Guess you like

Origin www.cnblogs.com/yourSixUncle/p/12170162.html