Managing route parameters
Route parameters give us the ability to capture values from the URL of an API endpoint. This is useful in many scenarios where we need to target specific entities, such as when requesting a TodoItem by its ID.
Route parameters are quite simple to add, and work using curly braces to define the parameters to be captured from the URL.
Let’s use a GET request as an example. In this request, the client requests a TodoItem with the ID:
app.MapGet("/todoitems/{id}", (int id) =>
{
var index = ToDoItems.FindIndex(x => x.Id == id);
if (index == -1)
{
return Results.NotFound();
}
return Results.Ok(ToDoItems[index]);
}); Like the generic GET request we created to fetch all todo items, this endpoint is sitting on the /todoitems route. However, it has an extra section appended...