Controller-based APIs

ApiController attribute

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

  • Attribute routing requirement

The [ApiController] attribute makes attribute routing a requirement.

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

  • Automatic HTTP 400 responses

The [ApiController] attribute makes model validation errors automatically trigger an HTTP 400 response.

  • Binding source parameter inference

A binding source attribute defines the location at which an action parameter's value is found.

Attribute
Binding source

[FromBody]

Request body

[FromForm]

Form data in the request body

[FromHeader]

Request header

[FromQuery]

Request query string parameter

[FromRoute]

Route data from the current request

[FromServices]

The request service injected as an action parameter

  • Multipart/form-data request inference

  • Problem details for error status codes

Action return types

  • Specific type

The ActionResult types represent various HTTP status codes.

  • IActionResult type

Synchronous action

Asynchronous action

  • ActionResult<T> type

ActionResult<T> return type enables you to return a type deriving from ActionResult or return a specific type.

Synchronous action

Asynchronous action

[ProducesResponseType] indicates the known types and HTTP status codes to be returned by the action. This attribute produces more descriptive response details from web API help pages generated by tools like Swagger.

Last updated

Was this helpful?