liveBook · Manning

Route constraint

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

A route constraint is a rule attached to a route parameter that determines whether a URL value satisfies the requirements for matching a route template.

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)

Constraint

Example

Description

Match examples

int

{qty:int}

Matches any integer

123, -123, 0

Guid

{id:guid}

Matches any Guid

d071b70c-a812-4b54-87d2-7769528e2814

decimal

{cost:decimal}

Matches any decimal value

29.99, 52, -1.01

min(value)

{age:min(18)}

Matches integer values of 18 or greater

18, 20

length(value)

{name:length(6)}

Matches string values with a length of 6

Andrew,123456

optional int

{qty:int?}

Optionally matches any integer

123, -123, 0, null

optional int max(value)

{qty:int:max(10)?}

Optionally matches any integer of 10 or less

3, -123, 0, null

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

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

How deep it goes
Explains parameter syntax, built-in types, combinations, optional values, matching behavior, and validation limits.
Examples
Uses integer, GUID, decimal, minimum, length, combined, and optional constraints with matching URL values.
What it assumes
Assumes basic minimal API endpoints and route parameters while introducing constraint syntax from simple templates.
What you can do after
Apply it via ASP.NET Core route templates to restrict matches and avoid binding incompatible URL values.
How it connects
Connects route templates, route parameters, minimal APIs, endpoint routing, optional parameters, and input validation.

Also explained in

Compare how 2 books explain route constraint →

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.