liveBook · Manning

WithParameterValidation

As explained in ASP.NET Core in Action, 3e

WithParameterValidation is a MinimalApis.Extensions endpoint extension method that adds parameter validation to a minimal API endpoint.

WithParameterValidation() adds a validation filter to a Minimal API endpoint. After model binding, the filter checks DataAnnotations on complex parameters before the handler runs. section 7.10.3

Validate a request model

Install MinimalApis.Extensions, then attach the extension to the endpoint: app.MapPost("/users", (UserModel user) => user.ToString()).WithParameterValidation(); A UserModel can declare requirements such as [Required], [StringLength(100)], and [EmailAddress]. Valid input reaches the handler; invalid input produces a 400 Bad Request Problem Details response containing validation errors. section 7.10.3

Keep these limitations in mind:

  • Do not place [Range] or similar attributes directly on a simple handler parameter such as int id; the validator checks properties of types, not handler-parameter attributes. section 7.10.3
  • Wrap simple values in a containing type, apply [AsParameters], add validation attributes to its properties, and use WithParameterValidation(). section 7.10.3

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains package setup, DataAnnotations behavior, automatic errors, and the AsParameters workaround, but not custom filter implementation.
Examples
Uses a validated UserModel and an AsParameters-wrapped identifier with a numeric range.
What it assumes
Assumes endpoint mapping and C# attributes; teaches package installation, filter placement, and wrapping simple values for validation.
What you can do after
Apply DataAnnotations validation to Minimal API endpoints through the MinimalApis.Extensions package.
How it connects
Connects Minimal APIs with model binding, validation filters, DataAnnotations, Problem Details, and AsParameters.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.