skip to content

In ASP.NET Core Minimal APIs, what does app.MapGroup("/api/v1/fleet") return, and how does it apply conventions once to many endpoints?

level: juniorimportance: must knowfreq 60%

answer

  1. one object, two roles
  2. prefix joined to each route pattern
  3. IEndpointRouteBuilder plus IEndpointConventionBuilder
  4. WithTags and AddEndpointFilter on the group
  5. not a middleware branch like app.Map

basics

~20 s

It returns a RouteGroupBuilder, both a route builder and a convention builder: endpoints mapped on it get the group prefix in front of their pattern, and WithTags or AddEndpointFilter on it reach every endpoint inside, nested groups included.

solid answer

~40 s

`MapGroup(prefix)` returns a `RouteGroupBuilder`, a sealed class that implements both `IEndpointRouteBuilder` and `IEndpointConventionBuilder`. Because it is a route builder, you call `MapGet`, `MapPost` or another `MapGroup` on it, and the group's prefix (route parameters and constraints included) is combined with each endpoint's own pattern. Because it is a convention builder, `WithTags("Fleet")`, `WithMetadata(...)` or `AddEndpointFilter<TimingFilter>()` on the group behave as if you had called them on every endpoint inside it, nested groups too, and they are applied before the endpoint's own conventions. The group only stores those conventions; they run when the endpoints are built, so they also reach endpoints mapped before the call. A group is not a middleware branch: there is no separate pipeline and `Request.PathBase` is untouched.

code

csharp · 15 lines
csharp
var fleet = app.MapGroup("/api/v1/fleet")
    .WithTags("Fleet")
    .AddEndpointFilter<TimingFilter>();

fleet.MapGet("/trucks", (IFleetStore store) => store.ListTrucksAsync());

var truck = fleet.MapGroup("/trucks/{truckId:int}");

// GET /api/v1/fleet/trucks/42/position
truck.MapGet("/position", (int truckId, IFleetStore store) =>
    store.GetPositionAsync(truckId));

// POST /api/v1/fleet/trucks/42/pings
truck.MapPost("/pings", (int truckId, Ping ping, IFleetStore store) =>
    store.AddPingAsync(truckId, ping));

go deeper

for a junior

Recall that MapGroup returns a RouteGroupBuilder, that its prefix is joined to each endpoint's pattern, and that WithTags or AddEndpointFilter on it reach every endpoint inside.

for a middle

Explain the two interfaces, how nested prefixes and route parameters combine, and why a convention called after MapGet still applies: conventions are stored and run when endpoints are built.

for a senior

Use groups to keep policy auditable: one group per API version or resource for shared filters and metadata, nested groups for sub-resources, and endpoint-level calls only to refine what the group set.

for a principal

Weigh convenience against visibility: deep nesting hides what an endpoint really carries, so agree where the team reads an endpoint's effective filters and metadata before groups multiply.

## What `MapGroup` returns `app.MapGroup("/api/v1/fleet")` is an extension method on `IEndpointRouteBuilder` (a `WebApplication` is one) and returns a **`RouteGroupBuilder`**. That sealed class implements two interfaces at once: - **`IEndpointRouteBuilder`**: you can call `MapGet`, `MapPost`, `MapPut`, `MapDelete` and `MapGroup` on it. Endpoints mapped there live in the group's own data source, which the outer builder picks up. - **`IEndpointConventionBuilder`**: every convention extension (`WithTags`, `WithMetadata`, `AddEndpointFilter`, `AddEndpointFilterFactory` and the rest) can be chained on the group, exactly as on the `RouteHandlerBuilder` that a single `MapGet` returns. A route group is therefore a **prefix plus a list of conventions** applied to every endpoint it contains. The `webapiaot` template's sample is written this way: `var todosApi = app.MapGroup("/todos");` followed by `todosApi.MapGet(...)`. ## Prefixes, nesting and route parameters In a truck-fleet tracker: ```csharp var fleet = app.MapGroup("/api/v1/fleet") .WithTags("Fleet") .AddEndpointFilter<TimingFilter>(); fleet.MapGet("/trucks", (IFleetStore store) => store.ListTrucksAsync()); var truck = fleet.MapGroup("/trucks/{truckId:int}"); truck.MapGet("/position", (int truckId, IFleetStore store) => store.GetPositionAsync(truckId)); ``` - The prefixes are **combined into one route pattern** per endpoint: the position endpoint matches `GET /api/v1/fleet/trucks/{truckId:int}/position`. - A **route parameter declared in a prefix** (`{truckId:int}`) is an ordinary route value, so handlers in the group and in its nested groups bind it like a parameter of their own template, and the `:int` constraint takes part in matching. - The prefix may be an **empty string**: `app.MapGroup("")` puts conventions on a set of endpoints without changing their URLs. - Nesting works because a group is itself an `IEndpointRouteBuilder`; a nested group inherits the outer prefix and the outer conventions. ## Conventions set once Calling a convention on a group has the same effect as calling it on every endpoint in it, ahead of anything an inner group or the endpoint adds. | Call on the group | Effect on each endpoint | |---|---| | `WithTags("Fleet")` | adds a `TagsAttribute` (`ITagsMetadata`), which OpenAPI uses to group operations | | `WithMetadata(obj)` | appends any metadata object that middleware and filters can read from the endpoint | | `AddEndpointFilter<TimingFilter>()` | wraps the handler in that filter | | `AddEndpointFilterFactory(...)` | runs a filter factory once per endpoint when it is built | Authorization, CORS and OpenAPI response metadata are applied to groups through the same mechanism; each has its own topic. ## When the conventions are applied The group only **stores** its conventions; calling `WithTags` touches no endpoint. When the endpoint data sources are first read (at the latest when routing handles the first request), each endpoint is built and receives, in this order: 1. the outermost group's conventions, 2. then each inner group's conventions, outside in, 3. then the endpoint's own conventions. Two consequences follow. A convention called on the group **after** `fleet.MapGet(...)` still reaches that endpoint, because nothing was applied at mapping time. And because the group's metadata lands earlier in the endpoint's metadata list, `EndpointMetadataCollection.GetMetadata<T>()` (which returns the most significant, last item) prefers what the endpoint itself set: an endpoint refines its group, never the other way round. ## What a group is not - **Not a middleware branch.** `app.Map("/api/v1/fleet", branch => ...)` forks the middleware pipeline and, by default, moves the matched segment into `Request.PathBase`. `MapGroup` forks nothing; its endpoints join the app's single routing table and run through the same middleware as every other endpoint. - **Not a path-base change.** `Request.PathBase` and `Request.Path` are untouched; the prefix is simply part of each endpoint's route pattern. - **Not consulted per request.** Request handling never asks the group anything; it sees only endpoints whose metadata and filters were copied from the group when they were built. ## What interviewers listen for - The return type and its two interfaces, not only "it adds a prefix". - That nested groups inherit prefixes and conventions, and that route parameters in a prefix bind in handlers. - That conventions are applied at build time, outer group first, so an endpoint can refine what its group set.

  • Can a handler in a nested group bind a route parameter that appears only in a group prefix?
    Yes. The prefixes are combined into one route pattern, so `{truckId:int}` declared in `fleet.MapGroup("/trucks/{truckId:int}")` is a route value for every endpoint below it. A handler parameter named `truckId` binds it as if it were in the endpoint's own template, and the `:int` constraint applies during matching.
  • Why would you call app.MapGroup with an empty string?
    To apply conventions to a set of endpoints without changing their URLs. `var ops = app.MapGroup("").AddEndpointFilter<AuditFilter>();` lets you map `ops.MapPost("/fleet/recalls", ...)` and `ops.MapPost("/fleet/retire", ...)` at their own paths while both carry the audit filter.
  • If WithTags is called on the group after the endpoints are mapped, do those endpoints get the tag?
    Yes. `RouteGroupBuilder` only records conventions in a list, and the list is applied when the group's endpoints are built, which happens when routing first reads the endpoint data sources. Mapping order and convention order therefore do not matter within startup code.

  • MapGroup returns a RouteHandlerBuilder, the same type MapGet returns.
  • MapGroup forks the middleware pipeline like app.Map and moves the prefix into PathBase.
  • Group conventions reach only the endpoints mapped after the convention call.
  • Handlers cannot bind route parameters declared in a group prefix.
  • A nested group starts clean and does not inherit the outer group's filters or tags.