Advertisement
Beginner ASP.NET Core, Endpoint Metadata, Route Constraints & Templates

What is a route template in ASP.NET Core, and how do route parameters like {id} work within one?

Short answer

A route template defines the URL pattern an endpoint matches, with curly-brace segments ({id}) representing route parameters extracted from the actual request URL and made available to the handler -- static segments (like 'products') must match literally, while parameter…

Save ASP.NET Core mock interview
Advertisement
AModel answer
A route template defines the URL pattern an endpoint matches, with curly-brace segments ({id}) representing route parameters extracted from the actual request URL and made available to the handler -- static segments (like 'products') must match literally, while parameter segments capture whatever value appears in that position of the URL.
app.MapGet("/products/{id}", (int id) => $"Product {id}");

// GET /products/42 -> id = 42
// GET /products/abc -> fails to bind since 'abc' isn't a valid int
Real-world example
An e-commerce API's route template /orders/{orderId}/items/{itemId} extracts both the order and item identifiers directly from the URL path, making them available as strongly-typed parameters to the handler.
Likely follow-ups
  • What happens if a URL segment doesn't match the expected parameter type?
  • How do you make a route parameter optional?
Related topics