liveBook · Manning

Chapter 7: Model binding and validation in minimal APIs

What you will learn in chapter 7 of ASP.NET Core in Action, 3e

This chapter introduces model binding in minimal APIs as the process that turns HTTP request data into handler inputs, then validates that user-provided input is acceptable.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduces model binding in minimal APIs as the process that turns HTTP request data into handler inputs, then validates that user-provided input is acceptable.

Key terms: model binding, HTTP request, POCO, endpoint handler, binding model, request body, URL, JSON, C# objects, endpoint handler methods

7.1 Extracting values from a request with model binding

Introduces model binding as the minimal API process that creates endpoint-handler arguments from HTTP request data before filters and handlers run.

Key terms: model binding, model binder, endpoint handler, binding, RoutingMiddleware, EndpointMiddleware, route parameters, filter pipeline, route values, query string values

7.2 Binding simple types to a request

This section explains how minimal API handlers bind simple values from routes, query strings, and headers, including custom types that can be parsed with TryParse.

Key terms: model binding, route parameter, route value, query string, handler parameter, [FromRoute], [FromQuery], [FromHeader], binding source, BadHttpRequestException

7.3 Binding complex types to the JSON body

This section explains how minimal API handlers automatically deserialize a JSON request body into one complex parameter, along with the JSON library, limits, and failure behavior involved.

Key terms: model binding, complex type, JSON, request body, System.Text.Json, deserialization, serialization, Newtonsoft.Json, trailing commas, property names

7.4 Arrays: Simple types or complex types?

This section explains how minimal APIs bind arrays from repeated query or header values, and when arrays instead bind from a JSON request body.

Key terms: query string, handler parameter, int[], 400 Bad Request, [FromQuery], [FromRoute], [FromHeader], binding name, simple types, TryParse

7.5 Making parameters optional with nullables

This section explains how minimal API handler parameters are required by default and how nullable parameters or default values allow missing request data.

Key terms: EndpointMiddleware, endpoint handler, 400 Bad Request, binding source, nullable, int?, nullable reference types, NRT, JSON body, null

7.6 Binding services and special types

This section introduces special endpoint-handler parameters that ASP.NET Core can bind because it recognizes them, provides them for file uploads, or resolves them as registered application services.

Key terms: well-known types, IFormFile, IFormFileCollection, application services, WebApplicationBuilder.Services, endpoint handlers, HttpContext, HttpRequest, ASP.NET Core, file uploads

7.6.1 Injecting well-known types

This section shows how minimal API handlers receive HttpContext and related well-known request types directly as parameters.

Key terms: well-known types, endpoint handler, HttpContext, HttpRequest, HttpResponse, CancellationToken, ClaimsPrincipal, Stream, PipeReader

7.6.2 Injecting services

This section explains how minimal API endpoint handlers receive registered dependency-injection services automatically and when explicit [FromServices] binding is useful.

Key terms: registered service, endpoint handler, dependency injection (DI) container, LinkGenerator, [FromServices], custom DI container, minimal APIs, WebApplicationBuilder, AddHttpLogging(), core services

7.6.3 Binding file uploads with IFormFile and IFormFileCollection

This section explains how minimal API handlers accept uploaded files with IFormFile or IFormFileCollection, and why file uploads require strict security and size precautions.

Key terms: IFormFile, IFormFileCollection, FileName, OpenReadStream, buffered, streaming

7.7 Custom binding with BindAsync

This section shows how minimal API parameter types can implement BindAsync to encapsulate custom request parsing and explains the response behavior when that binding fails.

Key terms: model binding, BindAsync, HttpContext, custom model binding, null, EndpointMiddleware, BadHttpRequestException, 400 error, 500 response, required parameter

7.8 Choosing a binding source

This section gives the precedence order minimal APIs use to select a binding source for each handler parameter and explains why binding does not fall back after a failure.

Key terms: binding source, explicit binding source, FromRoute, FromQuery, FromBody, well-known type, binding fails, optional parameter, 400 Bad Request

7.9 Simplifying handlers with AsParameters

This section shows how [AsParameters] groups a minimal API handler’s bound inputs into one type for a clearer signature, then introduces why incoming data must be validated.

Key terms: [AsParameters], endpoint handler, record struct, [From*] attributes, constructor parameters, well-known types, model binding, validation, malicious data, SQL injection

7.10 Handling user input with model validation

Introduces the need for validation in minimal APIs and previews DataAnnotations and filter-based parameter validation.

Key terms: validation, user input, minimal APIs, filters, nonprescriptive hooks, DataAnnotations, endpoint handler parameters

7.10.1 The need for validation

This section explains why minimal API request data must be validated early and what kinds of malicious and accidental input problems validation addresses.

Key terms: validation, endpoint handler parameters, request, user-provided data, minimal API filter pipeline, filters, format, range, required, optional

7.10.2 Using DataAnnotations attributes for validation

This section shows how DataAnnotations and IValidatableObject express validation rules for minimal API binding models, while clarifying their limits and the need to run validation separately.

Key terms: DataAnnotations attributes, binding model, [Required], [StringLength], [Range], [EmailAddress], [CreditCard], [MinLength], [Phone], IValidatableObject

7.10.3 Adding a validation filter to your minimal APIs

This section explains how to add DataAnnotations-based validation to minimal API endpoints with a filter, including a workaround for validating simple values.

Key terms: minimal APIs, filter pipeline, cross-cutting concern, DataAnnotations, FluentValidation, MinimalApis.Extensions, WithParameterValidation(), 400 Bad Request, Problem Details, [Range]

7.11 Summary

This summary recaps how minimal APIs bind handler parameters from requests, customize binding and signatures, and validate incoming data.

Where this is used later

Concepts explained in this chapter

← Chapter 6: Mapping URLs to endpoints using routing Chapter 8: An introduction to dependency injection →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.