Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core FromForm And FromBody Same Action

Can I have both FromForm and FromBody attributes to action ?

public IActionResult Index([FromBody][FromForm] Person model){ ..... } 
like image 471
Petko Pasev Avatar asked May 21 '18 17:05

Petko Pasev


People also ask

What is the difference between FromForm and FromBody?

[FromForm] - Gets values from posted form fields. [FromBody] - Gets values from the request body.

Can we use FromBody with Httpget?

Please note that we are able to send [FromBody] parameter in HTTP GET Request input.

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

How does model binding work in asp net core application?

Model binding allows controller actions to work directly with model types (passed in as method arguments), rather than HTTP requests. Mapping between incoming request data and application models is handled by model binders.


1 Answers

No, it's not possible.

The FromForm attribute is for incoming data from a submitted form sent by the content type application/x-www-form-urlencoded while the FromBody will parse the model the default way, which in most cases are sent by the content type application/json, from the request body.

like image 125
Marcus Höglund Avatar answered Oct 16 '22 14:10

Marcus Höglund