liveBook · Manning

ModelState

As explained in Building Web APIs with ASP.NET Core

ModelState is the ASP.NET Core representation of model-binding and validation state, including associated errors for an HTTP request.

ModelState is ASP.NET Core’s record of model-binding and validation results for an HTTP request. It becomes valid when no errors occur and invalid when binding or validation reports errors. section 6.2.1 section 6.4

Figure 6.3 Model binding and validation lifecycle with the [ApiController] attribute
06-03

Follow the request lifecycle from model binding through validation, automatic error handling, and action execution. section 6.2.1

With [ApiController], ModelState normally works automatically:

  • Model binding converts request values into action parameters and complex types; binding failures produce an HTTP 400 response. section 6.2.1
  • Validation checks built-in or custom validation attributes and IValidatableObject, recording the results in ModelState. section 6.2.1 section 6.4
  • An invalid ModelState prevents the action method from running and returns HTTP 400 automatically. section 6.2.1

Customizing ModelState handling

Configure ApiBehaviorOptions with SuppressModelStateInvalidFilter = true when an endpoint must inspect invalid input itself. The action can then test ModelState.IsValid, create ValidationProblemDetails(ModelState), and return a status code such as HTTP 501 for an invalid pageSize. A ManualValidationFilterAttribute can apply the same opt-out selectively to a controller action while preserving [ApiController]. section 6.2.3 section 6.2.3 section 6.3.4

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains the binding-validation lifecycle and manual configuration, but defers the internal structure of ModelState entries.
Examples
Uses custom validation messages, pageSize-specific HTTP 501 handling, and conditional HTTP 403 or HTTP 400 responses.
What it assumes
Assumes HTTP request handling and API actions, while introducing model binding, validation attributes, and automatic ApiController behavior.
What you can do after
Diagnose invalid request input automatically or configure manual inspection for customized validation responses.
How it connects
Connects model binding, data validation, validation attributes, IValidatableObject, ApiController, and HTTP error handling.

Also explained in

Compare how 3 books explain ModelState →

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.