ASP.NET Core routing chooses endpoints from route templates, HTTP methods, and constraints—not from C# overload resolution alone. Constraints narrow candidates and should disambiguate shapes, not perform business validation.
```csharp
[ApiController]
[Route("api/orders")]
public sealed class OrdersController : ControllerBase
{
[HttpGet("{id:guid}")]
public IActionResult GetById(Guid id) => Ok(id);
[HttpGet("number/{number:int:min(1)}")]
public IActionResult GetByNumber(int number) => Ok(number);
}
```
Routes such as two `[HttpGet("{value}")]` actions—one taking `int`, another `string`—are ambiguous unless templates include constraints or distinct static segments. C# method signatures do not make the URLs unique. Use clear resource-oriented paths, apply `[FromRoute]`, `[FromQuery]`, or `[FromBody]` when binding could be unclear, and return normal validation errors after routing for domain rules. Integration-test representative and boundary URLs because link generation and endpoint selection are runtime behavior.
Prince Yadav · · 0 likes
Route constraints restrict which URL values match a route parameter, and they also decide which action is chosen when several routes look similar. In ASP.NET Core, there is no C#-style "overload resolution" by parameter type for actions: routing chooses one endpoint from the URL, HTTP method and constraints, and fails if the choice is ambiguous.
Common constraints
- {id:int}, {id:long}, {id:guid}, {flag:bool}, {date:datetime}, {price:decimal}
- {name:alpha}, {code:length(6)}, {name:minlength(3)}, {age:range(18,99)}, {slug:regex(^[a-z0-9-]+$)}
- {id:int:min(1)} combines constraints; {id?} makes it optional.
Using constraints to "overload" by format:
[ApiController]
[Route("api/products")]
public class ProductsController : ControllerBase
{
[HttpGet("{id:int}")]
public IActionResult GetById(int id) => Ok($"by id {id}");
[HttpGet("{sku:regex(^[A-Z]{{3}}-\\d+$)}")]
public IActionResult GetBySku(string sku) => Ok($"by sku {sku}");
[HttpGet("{slug}")]
public IActionResult GetBySlug(string slug) => Ok($"by slug {slug}");
}
- /api/products/42 matches GetById.
- /api/products/ABC-123 matches GetBySku.
- /api/products/blue-pen matches GetBySlug.
Without constraints, these three templates would be ambiguous and throw AmbiguousMatchException.
How endpoint selection works
- Literal segments are preferred over parameters; parameters with constraints are preferred over unconstrained ones; catch-all parameters ({**path}) have the lowest priority. You can adjust with the Order property on route attributes.
- The HTTP method (HttpGet vs HttpPost) is part of the match, so the same template can serve different verbs.
- Query string parameters do not participate in route matching, so two actions differing only by query parameters are ambiguous.
Important guidance
- Constraints are for disambiguation, not input validation. A failed constraint returns 404, which is misleading for invalid input. Validate values (for example, a range) in the model or action and return 400.
- Avoid regex-heavy routes on hot paths and keep URLs predictable.
- You can create custom constraints by implementing IRouteConstraint and registering them in RouteOptions.ConstraintMap.
- Minimal APIs use the same constraint syntax: app.MapGet("/products/{id:int}", ...).