A route constraint is a rule attached to a route parameter. It narrows which URL values can match a route template, helping ASP.NET Core choose the correct endpoint before the handler runs. section 6.3.3
Constrain an integer parameter
Use a colon after the parameter name: app.MapGet("/fruit/{id:int}", (int id) => "Hello world!"). The route matches integer values such as 123, but not noninteger values. Without the constraint, a noninteger URL value can cause an exception when it is bound to the integer parameter. section 6.3.3
Common constraints include:
- int, guid, and decimal require values convertible to those types. section 6.3.3
- min(value) requires an integer at least the specified value, while length(value) requires a string of the specified length. section 6.3.3
- Combine constraints with colons, and append ? when the constrained parameter is optional, such as {qty:int:max(10)?}. section 6.3.3
Table 6.2 A few route constraints and their behavior when applied (view table figure)
Compare constraint syntax, accepted values, and matching examples in a compact reference table. section 6.3.3
Routing checks every constraint when matching a URL. If any constraint fails, the route is not a match and its endpoint is not executed. Use constraints for strict URL requirements or to distinguish similar routes, not for general input validation: invalid input can otherwise appear as a confusing 404 response. Built-in constraints also assume invariant culture. section 6.3.3 section 6.5

