MapGroup creates a route group with a shared prefix. Endpoints added to the returned RouteGroupBuilder inherit that prefix, keeping related API routes shorter and less repetitive. section 5.5
Group fruit endpoints
var fruitApi = app.MapGroup("/fruit"); fruitApi.MapGet("/", () => _fruit); fruitApi.MapGet("/{id}", (string id) => GetFruit(id)); These definitions produce routes beginning with /fruit, while the individual mappings only describe the remaining path. section 5.5
Use a route group when several endpoints share organization or behavior:
- Apply one prefix to every endpoint in the group. section 5.5
- Apply an endpoint filter once with AddEndpointFilter() instead of repeating it on each endpoint. section 5.5
- Nest groups to combine prefixes, such as /fruit followed by /citrus. section 5.5
- Pass / when filtering is useful but an additional route prefix is not. section 5.5
Figure 5.11 Using route groups to simplify the definition of endpoints. You can create a route group by calling MapGroup() and providing a prefix. Any endpoints created on the route group inherit the route template prefix, as well as any filters added to the group.
Visualizes how a group shares its route-template prefix and inherited filters across endpoint definitions. section 5.5
