Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the [ApiController] attribute do? [duplicate]

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?

like image 357
ilyas varol Avatar asked Mar 09 '21 11:03

ilyas varol


People also ask

What is the purpose of ApiController attribute?

The [ApiController] attribute applies inference rules for the default data sources of action parameters. These rules save you from having to identify binding sources manually by applying attributes to the action parameters.

What is an ApiController?

Web API Controller is similar to ASP.NET MVC controller. It handles incoming HTTP requests and send response back to the caller. Web API controller is a class which can be created under the Controllers folder or any other folder under your project's root folder.

Is ApiController needed?

In ASP.NET Core this is simply not necessary anymore. The Controller base class can be used for actions that return HTML from Razor Views or JSON (with output formatters XML and other formats are possible as well).

What is the difference between ApiController and controller?

They work similarly in Web API, but controllers in Web API derive from the ApiController class instead of Controller class. The first major difference you will notice is that actions on Web API controllers do not return views, they return data. ApiControllers are specialized in returning data.


1 Answers

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");
}
like image 59
haldo Avatar answered Oct 05 '22 21:10

haldo