liveBook · Manning

Model binding

As explained in Building Web APIs with ASP.NET Core

Model binding is the ASP.NET Core mechanism that maps incoming request data to action-method parameters and performs framework-managed type conversion and default handling.

Model binding turns incoming HTTP request values into the typed parameters an ASP.NET Core action method expects. Request values arrive as strings; the framework retrieves and converts them automatically, rather than requiring the action method to parse each one. section 6.1.1

A board-game list request

For GET /BoardGames?pageIndex=2&pageSize=50, model binding converts "2" and "50" to integers and supplies them to the Get action. If sorting or filtering parameters are omitted, their configured defaults can be used. An unconvertible pageIndex, such as "string", is a binding error rather than a value the action must interpret. section 6.1.1 section 6.1

public async Task<RestDTO<BoardGame[]>> Get(
 int pageIndex = 0,
 int pageSize = 10,
 string? sortColumn = "Name",
 string? sortOrder = "ASC",
 string? filterQuery = null)

The action signature shows the expected parameter types and defaults for omitted pagination, sorting, and filtering values. section 6.1.1

Binding also works with a complex type such as a request DTO: each property is bound and validated like an individual parameter. This keeps a growing list of pagination, sorting, and filtering inputs in one class. Put [FromQuery] on the DTO parameter when those values should continue to come from the query string. Property initializers provide defaults; the example also uses [DefaultValue] so generated Swagger documentation can display them. section 6.1.5

Conversion is only the first check. Validation attributes can impose rules on parameters or DTO properties: for example, [Range(1, 100)] on PageSize rejects an excessive page size without a manual conditional. Custom validation attributes and IValidatableObject can handle further checks. section 6.1.2 section 6.4

With [ApiController], the framework binds inputs, then validates them and records validation results in ModelState. A binding failure or invalid model produces HTTP 400 before the action runs. This is convenient for ordinary invalid requests; checking ModelState manually is an option when the validation response needs customization. section 6.2.1 section 6.4

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Walks through string conversion, defaults, DTO property binding, validation, and request flow without implementing a binder.
Examples
Uses a paginated GET request, a RequestDTO with [FromQuery], and a [Range] page-size limit.
What it assumes
Assumes familiarity with HTTP requests and C# action parameters while explaining how raw values become .NET types.
What you can do after
Apply it via ASP.NET Core action parameters, query-bound DTOs, and validation attributes to accept typed inputs and reject invalid ones.
How it connects
Connects model binding to action methods, DTOs, data annotations, ModelState, and [ApiController] validation.

Also explained in

Compare how 3 books explain model binding →

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.