Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

702
Views
What does the [ApiController] attribute do?

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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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");
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!