StaffCoder
.NET
.NET.NET 10
12 modules · 48 challengesSign up
Module 3 · Minimal APIs and Routing

Route Constraints For Order Lookup

easy100%1 of 1 solved

Problem statement

Map two order lookups with route constraints, so a badly formed order number or tracking code is turned away by routing and never reaches the handler.

Requirements

The order store and the two handlers in OrderHandlers are already provided. Your task has one part: map the two lookups in MapOrderEndpoints in OrderLookupApi/Endpoints/OrderEndpoints.cs.

A route constraint, written as {name:constraint} in the route, decides whether a URL segment matches at all. When it does not match, routing answers 404 and the handler never runs. Without a constraint, a value that cannot become the handler's parameter type is answered with 400 instead.

OrderHandlers (provided)

  • GetOrderById takes an int orderId and answers 200 with the order, or 404 when no order has that id
  • GetOrderByTrackingCode takes a Guid trackingCode and answers the same way
  • Each handler counts its calls, shown at GET /diagnostics/handler-invocations

OrderEndpoints

  • GET /orders/{orderId} is handled by GetOrderById. Only a whole number that fits in an int matches, so abc, 12.5 and 2147483648 get a 404 from routing
  • GET /orders/tracking/{trackingCode} is handled by GetOrderByTrackingCode. Only a GUID matches, so not-a-guid or a cut-off GUID gets a 404 from routing
  • A well-formed value always reaches its handler, even when no order has it
Sample Requests & Responses

GET /orders/1001

Response (200 OK):

{
  "orderId": 1001,
  "trackingCode": "4d2a7c18-9f31-4a5b-8c6d-1e2f3a4b5c6d",
  "customer": "Dana Whitfield",
  "status": "Shipped",
  "total": 149.95
}

GET /orders/tracking/4d2a7c18-9f31-4a5b-8c6d-1e2f3a4b5c6d

Response (200 OK), with the same order.

GET /orders/abc and GET /orders/tracking/not-a-guid

Response (404 Not Found) for each, from routing.

GET /diagnostics/handler-invocations after the four requests above

Response (200 OK), showing that only the two well-formed requests reached a handler:

{ "orderById": 1, "orderByTrackingCode": 1 }

More in Minimal APIs and Routing

See the module
Book Catalog EndpointsThis problem covers mapping the list, detail and create endpoints of a resource as minimal APIs, each answering with the right status code, and pointing the caller at what was just created.100%1 of 1 solvedeasySearch Endpoint Query BindingThis problem covers taking search filters from the query string as typed handler parameters, each falling back to a default when the caller leaves it out, and turning away a value of the wrong type.100%1 of 1 solvedeasyTask Tracker API With Route GroupsThis problem covers mapping the five endpoints of a task tracker in one route group with typed results, so listing can be filtered, creating points at what it made, and updating or deleting something that is not there says so.100%1 of 1 solvedeasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.