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
