liveBook · Manning

MapGroup

As explained in ASP.NET Core in Action, 3e

MapGroup is an ASP.NET Core endpoint-mapping method that creates a route group with a specified route prefix.

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.
CH05_F11_Lock3

Visualizes how a group shares its route-template prefix and inherited filters across endpoint definitions. section 5.5

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains prefixes, filters, nesting, and a fruit API rewrite, without detailing MapGroup internals.
Examples
Uses a fruit API, nested citrus-style prefixes, validation filters, and a grouped endpoint listing.
What it assumes
Assumes familiarity with WebApplication endpoint mapping and introduces RouteGroupBuilder through a small fruit API.
What you can do after
Apply it via ASP.NET Core minimal APIs to reduce duplicated route prefixes and filter configuration.
How it connects
Connects minimal APIs with RouteGroupBuilder, endpoint filters, route templates, and WebApplication.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.