An API has many paths, and each path has its own function. When a request arrives, ASP.NET compares its path with the path of every route, and calls the function whose path matches exactly. So /products/count is not /products: it needs a route of its own.
A route can answer any object as JSON. new { ... } makes an object on the spot, and each name in it becomes a key. For example, a shop could tell its opening hours like this:
app.MapGet("/hours", () => {
return Results.Json(new { open = 9, close = 18 });
});
This answers {"open": 9, "close": 18}. To answer how many items a list has, use its .Count instead of writing the number yourself: the answer stays right when items are added or removed. A path that no route matches, like /contact, gets 404 Not Found with an empty body: ASP.NET has no function to call.
Your app must answer:
GET /products/count → 200 {"count": 4}
GET /about → 200 {"name": "Corner Shop", "currency": "USD"}
GET /contact → 404 (empty body)
GET /products → 200 [{"id": 1, "name": "Mug", "price": 12, "category": "kitchen"}, … all 4 products]
GET / → 200 Welcome to the shop!
Your task: add two routes with app.MapGet. GET /products/count answers the number of products as {"count": …}, computed with products.Count. GET /about answers the shop's name, "Corner Shop", and its currency, "USD".
Assignment
Follow the Coding Tutorial to add the /products/count and /about routes.
Hint
Each new route has the same shape as the /products route: app.MapGet with its path and () => {, the return, then });. Put each new { ... } object inside Results.Json(...).
Free plan: 30 tutor messages. PRO is unlimited.
