Mvc: For types annotated with ApiControllerAttribute, consider making complex type parameters FromBody by default

Created on 19 Sep 2017  路  2Comments  路  Source: aspnet/Mvc

As part of producing, more opinionated APIs via ApiControllerAttribute we can assume any complex type parameter in your action without binding information is interpreted as FromBody. This will reduce the amount of typing \ clutter in user code:

```C#
[ApiController]
public class OrdersController
{
// ...
[HttpPost]
public IActionResult Create(Book book); // Assumes FromBody

[HttpPut]
public IActionResult Update([FromQuery] Book book); // Opt out
}
```

We could add a switch to ApiControllerOptions to disable this behavior globally.

3 - Done

Most helpful comment

The proposal here is to do:

  • Complex types are [FromBody] by default
  • Parameters that match the route are [FromRoute] by default
  • Any other parameters are [FromQuery] by default

We think this is the most reasonable possible convention we could have and guides you towards good practices.

All 2 comments

The proposal here is to do:

  • Complex types are [FromBody] by default
  • Parameters that match the route are [FromRoute] by default
  • Any other parameters are [FromQuery] by default

We think this is the most reasonable possible convention we could have and guides you towards good practices.

No one has raised a significant objection to this so we're going to start working on it 馃憤

Was this page helpful?
0 / 5 - 0 ratings