A route parameter is a named variable inside a URL pattern. When the request path matches, ASP.NET Core extracts the variable value and makes it available to the endpoint handler. section 5.2.1 section 6.3.1
Filtering people by name
The route "/person/{name}" matches "/person/Al" and binds name to "Al". The handler can then filter people whose first names start with that value, returning matching objects as JSON. section 5.2.1
Use route parameters to define flexible URL patterns:
- Write a parameter in braces, such as {category} or {name}; its URL segment may vary, but the segment is required unless marked optional. section 6.3.1
- Combine literal segments and parameters, such as /{category}/{name}, to match product paths like /bags/rucksack-a. section 6.3.1
- Add ? to make a final parameter optional, or use =value to supply a default when the URL omits it. section 6.3.2
- Treat optional and default parameters sparingly because they can make APIs harder to understand; use constraints to restrict matching, not to perform validation. section 6.5
Listing 5.1 A minimal API that uses a value from the URL
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
WebApplication app = builder.Build();
var people = new List<Person> #1
{ #1
new("Tom", "Hanks"), #1
new("Denzel", "Washington"), #1
new("Leondardo", "DiCaprio"), #1
new("Al", "Pacino"), #1
new("Morgan", "Freeman"), #1
}; #1
app.MapGet("/person/{name}", (string name) => #2
people.Where(p => p.FirstName.StartsWith(name))); #3
app.Run();
Inspect a complete minimal API that declares a {name} parameter, binds it to a string, and uses it in endpoint logic. section 5.2.1





