RouteGroupBuilder is returned by MapGroup() and organizes related minimal API endpoints under a shared route prefix and shared endpoint conventions. section 5.5
Use a RouteGroupBuilder to reduce repeated route and filter configuration:
- Call app.MapGroup("/fruit") to apply /fruit to every endpoint created on the returned builder. section 5.5
- Call the same Map* extension methods on the group as on WebApplication. section 5.5
- Call AddEndpointFilter() once on the group to apply a filter to all endpoints it contains. section 5.5 section 5.6
- Nest groups to combine prefixes, such as /fruit followed by /citrus; use / when grouping without adding a prefix. section 5.5
Group fruit endpoints and validation
RouteGroupBuilder fruitApi = app.MapGroup("/fruit"); RouteGroupBuilder fruitApiWithValidation = fruitApi.MapGroup("/") .AddEndpointFilter(ValidationHelper.ValidateIdFactory); fruitApiWithValidation.MapGet("/{id}", (string id) => ...); fruitApiWithValidation.MapPost("/{id}", (Fruit fruit, string id) => ...); 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.
See how a group supplies a shared route-template prefix and inherited endpoint filters. section 5.5
