liveBook · Manning

Automatic model state validation

As explained in Building Web APIs with ASP.NET Core

Automatic model state validation is the framework behavior that short-circuits an API action with a client-error response when ModelState is invalid.

Automatic model state validation short-circuits an API action when ModelState is invalid, returning a BadRequestObjectResult before the action can inspect the errors. section 6.2.3

Preserve [ApiController] features while disabling only this filter through ApiBehaviorOptions. The action can then inspect ModelState and choose a context-specific response. section 6.2.3

A practical customization sequence is:

  • Inspect validation keys and return a tailored status, such as HTTP 501 for a PageSize error and HTTP 400 otherwise. section 6.2.3

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains the validation lifecycle, configuration switch, action checks, and conditional responses without detailing framework filter internals.
Examples
Uses Program.cs configuration and a DomainsController action that distinguishes PageSize errors from other validation failures.
What it assumes
Assumes familiarity with API controllers and ModelState while demonstrating configuration and manual validity checks.
What you can do after
Operate/configure ASP.NET Core validation behavior and return tailored responses from actions with invalid ModelState.
How it connects
Connects ApiController, model binding, ModelState, validation, action filters, and HTTP status codes.

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.