What does the [ApiController] attribute do? [duplicate]
Asked Answered
B

1

31

I've noticed it is the same thing if this attribute is used or not. Am I wrong?

As an example:

[Route("[controller]")]
[ApiController]
public class DataTablesController: ControllerBase
{
    [HttpGet]
    public IActionResult Test()
    {
        return Ok("test");
    }
}

Nothing happened when I removed the [ApiController] attribute.

In the Microsoft documentation, I found this explanation:

Indicates that a type and all derived types are used to serve HTTP API responses.
Controllers decorated with this attribute are configured with features and behavior targeted at improving the developer experience for building APIs.
When decorated on an assembly, all controllers in the assembly will be treated as controllers with API behavior.

What is that API behaviors? And why should we use it?

Baudoin answered 9/3, 2021 at 11:15 Comment(0)
H
47

The [ApiController] attribute enables a few features including attribute routing requirement, automatic model validation and binding source parameter inference.

This was taken straight from the MS docs Create web APIs with ASP.NET Core:

The [ApiController] attribute can be applied to a controller class to enable the following opinionated, API-specific behaviors:

  • Attribute routing requirement
  • Automatic HTTP 400 responses
  • Binding source parameter inference
  • Multipart/form-data request inference
  • Problem details for error status codes

The Problem details for error status codes feature requires a compatibility version of 2.2 or later. The other features require a compatibility version of 2.1 or later.

Some details on the features below:

Attribute routing

Attribute routing will be required if you use [ApiController], eg:

[ApiController]
[Route("[controller]")]
public class DataTablesController: ControllerBase

Actions are inaccessible via conventional routes defined by UseEndpoints, UseMvc, or UseMvcWithDefaultRoute in Startup.Configure

Automatic Http 400 responses

Adds an action filter to return 400 response if the ModelState fails validation. You no longer need to write this in your actions, it will be handled automatically:

if (!ModelState.IsValid)
{
    return BadRequest(ModelState);
}

Binding source parameter inference

Again, from the linked docs:

A binding source attribute defines the location at which an action parameter's value is found. The following binding source attributes exist: [FromBody], [FromForm], [FromHeader], [FromQuery], [FromRoute], [FromServices]

Multipart/form-data request inference

The [ApiController] attribute applies an inference rule when an action parameter is annotated with the [FromForm] attribute. The multipart/form-data request content type is inferred.

An example using binding source parameter inference:

[HttpPost]
public IActionResult Test([FromForm] Model model)
{
    return Ok("test");
}
Helvetic answered 9/3, 2021 at 11:32 Comment(1)
If someone is interested in the source, the concrete conventions it executes can be found in ApiBehaviorApplicationModelProvider (source), and behavior can be customized via ApiBehaviorOptions (source), which is accessible from the container through the options pattern.Kosey

© 2022 - 2024 — McMap. All rights reserved.