liveBook · Manning

ApiController

As explained in Building Web APIs with ASP.NET Core

The ApiController attribute is an ASP.NET Core attribute that enables API-controller conventions, including automatic handling of model-binding and validation failures.

Apply [ApiController] to an ASP.NET Core controller to enable web API conventions. One important convention handles invalid request input automatically: the framework returns an HTTP 400 response before the action method runs. [ApiController] is separate from attributes such as [Route] and [HttpGet], which specify routes and HTTP verbs. section 1.2.3 section 6.2.1

For each request, the relevant validation flow is:

  • Model binding converts incoming values into action parameters. A binding failure produces an HTTP 400 response. section 6.2.1
  • Validation checks the bound input against built-in or custom rules and records errors in ModelState. section 6.2.1 section 6.4
  • If ModelState is valid, the action runs. If it is invalid, [ApiController] returns HTTP 400 without calling the action. section 6.2.1

An invalid query value

If a client sends the word "string" for a numeric pageIndex parameter, the response has status 400 and identifies pageIndex and its rejected value in a validation-error body. The action does not need code to construct that response. section 6.2

Automatic HTTP 400 responses are useful until an action needs a different response for a particular validation failure. Removing [ApiController] would let the action inspect invalid ModelState, but would also discard its other conventions. Instead, suppress the automatic invalid-ModelState filter, then check ModelState in the action and choose a response. The shown configuration applies to all controllers; a custom action-level filter convention is another way to limit the change. section 6.2.3

builder.Services.Configure<ApiBehaviorOptions>(options =>
options.SuppressModelStateInvalidFilter = true);
 
var app = builder.Build();

Place this configuration in Program.cs before building the app to suppress automatic invalid-ModelState responses while retaining [ApiController]. Actions must then handle invalid ModelState where a response is needed. section 6.2.3

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Traces binding and validation through ModelState, then shows how to suppress automatic HTTP 400 responses for manual handling.
Examples
Shows an invalid pageIndex response and Program.cs configuration for suppressing the automatic invalid-ModelState filter.
What it assumes
Introduces the model-binding and validation lifecycle before explaining ModelState; uses controller actions and Program.cs configuration.
What you can do after
Operate/configure it to retain automatic validation or inspect ModelState manually when an action needs a custom response.
How it connects
Connects model binding, ModelState, data validation, HTTP status codes, and ASP.NET Core controller attributes.

Also explained in

Compare how 2 books explain ApiController →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.