RouteValuesDictionary stores the names and values captured by route parameters in an HTTP request. HttpRequest.RouteValues exposes it to the selected endpoint as an enumerable collection of KeyValuePair<string, object> entries. section 13.1.5
Useful operations include:
- Read a value with the string indexer, such as context.Request.RouteValues["country"]. section 13.1.5
- Inspect Keys, Values, and Count, or test for a name with ContainsKey. section 13.1.5
- Enumerate entries when an endpoint needs to display or process every captured route parameter. section 13.1.5
Three captured segments
A route pattern of {first}/{second}/{third} matches /apples/oranges/cherries. Iterating context.Request.RouteValues produces first: apples, second: oranges, and third: cherries. section 13.1.5
Listing 13.8 Using segment variables in the Program.cs file in the Platform folder
using Platform;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("{first}/{second}/{third}", async context => {
await context.Response.WriteAsync("Request Was Routed\n");
foreach (var kvp in context.Request.RouteValues) {
await context.Response
.WriteAsync($"{kvp.Key}: {kvp.Value}\n");
}
});
app.MapGet("capital/uk", new Capital().Invoke);
app.MapGet("population/paris", new Population().Invoke);
app.Run();
Runnable endpoint code shows route-variable enumeration in Program.cs. section 13.1.5
