skip to content

In ASP.NET Core routing, how do the catch-all parameters {*path} and {**path} differ, and where in a template may one appear?

level: middleimportance: should knowfreq 38%

answer

  1. rest of the path, slashes included
  2. identical when matching
  3. differ when generating links
  4. foo/my%2Fpath vs foo/my/path
  5. last segment only, never optional

basics

~20 s

Both capture the rest of the path, slashes included, and match identically. They differ in URL generation: {*path} encodes a slash in the value as %2F, while {**path} keeps it, so the path round-trips. Either must be the template's last segment.

solid answer

~40 s

A catch-all parameter binds everything after its position, so `wiki/{**path}` matches `/wiki/fantasy/tolkien/maps`, giving `path = "fantasy/tolkien/maps"`, and also `/wiki` itself, because a catch-all can match the empty string. When matching, one star and two stars behave the same. The difference shows when a link is generated from route values: per the routing docs, `foo/{*path}` with `path = "my/path"` produces `foo/my%2Fpath`, while `foo/{**path}` produces `foo/my/path`. Use `**` whenever the value is itself a path you want to round-trip. The parser enforces placement: a catch-all must be the last segment, cannot be optional, and cannot share a segment with literals or other parameters. Catch-alls are the least specific templates, so more specific routes win when both match.

code

csharp · 17 lines
csharp
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/wiki/{**path}", (string? path) =>
    Results.Ok(new { page = path ?? "home" }))
    .WithName("WikiPage");

app.MapGet("/wiki-links", (LinkGenerator links) => new
{
    // "/wiki/fantasy/tolkien" - ** keeps the slash
    deep = links.GetPathByName("WikiPage", new { path = "fantasy/tolkien" })
});

// app.MapGet("/wiki/{**path}/history", ...) would throw RoutePatternException:
// "A catch-all parameter can only appear as the last segment of the route template."

app.Run();

go deeper

for a junior

Recall that a catch-all {*path} or {**path} takes the rest of the URL including slashes and must be the last segment.

for a middle

Explain that both forms match the same requests and differ in link generation, where * encodes the slash as %2F and ** keeps it.

for a senior

Choose ** for path-like values that round-trip into links, keep catch-alls least specific behind literal routes, and treat encoded slashes in captured values deliberately.

for a principal

Decide where catch-alls belong at all, such as a wiki or CMS area, and keep them confined to a prefix so they never swallow URLs other teams will add later.

## What a catch-all parameter does An ordinary route parameter such as `{slug}` captures **one** path segment and never a `/`. A **catch-all parameter**, written with a `*` or `**` prefix, captures **the rest of the path** from its position onward, including the slashes. A bookshop that hosts a reader wiki is the typical use: ```csharp app.MapGet("/wiki/{**path}", (string? path) => Results.Ok(new { page = path ?? "home" })); ``` - `/wiki/fantasy/tolkien/maps` gives `path = "fantasy/tolkien/maps"`. - `/wiki/authors` gives `path = "authors"`. - `/wiki` itself also matches, because a catch-all can match the **empty string**; the handler then sees no value, hence `string?`. ## One star versus two stars For **incoming requests** `{*path}` and `{**path}` behave identically: same matching, same captured value. The difference appears only when ASP.NET Core **generates a URL** from route values, through `LinkGenerator`, `Url.Action`, `CreatedAtRoute` or a tag helper. | Template | Route value | Generated path | |---|---|---| | `foo/{*path}` | `path = "my/path"` | `foo/my%2Fpath` | | `foo/{**path}` | `path = "my/path"` | `foo/my/path` | With a single star the generator treats the value as **one** opaque segment and percent-encodes the `/` as `%2F`. With two stars it treats the value as a **path** and keeps the separators, so a value captured from `/wiki/fantasy/tolkien` regenerates the same URL. In the routing source this is the `EncodeSlashes` flag on `RoutePatternParameterPart`: `true` for `*`, `false` for `**`. The practical rule: if the captured value is a path that will ever be put back into a link, use `**`. A wiki page link built from `{*path}` would come out as `/wiki/fantasy%2Ftolkien`, which some proxies and servers refuse or decode differently. ## Where a catch-all may appear The template parser rejects misplaced catch-alls when the endpoint is registered, each with its own message: 1. **Last segment only.** "A catch-all parameter can only appear as the last segment of the route template." So `wiki/{**path}/history` is invalid; put the action first, as in `wiki/history/{**path}`. 2. **Never optional.** "A catch-all parameter cannot be marked optional." It already matches the empty string, so `{**path?}` is both redundant and invalid. 3. **Alone in its segment.** A segment that mixes a literal or another parameter with a catch-all, such as `docs-{**path}`, is rejected. A catch-all can still carry a constraint, for example `{**path:nonfile}` (the pattern `MapFallback` uses is `{*path:nonfile}`), to keep requests whose last segment looks like a file name, such as `cover.png`, away from a page handler. ## Catch-alls and other routes Because it matches almost anything below its prefix, a catch-all is the **least specific** kind of segment. When `/wiki/{**path}` and `/wiki/random` both match `/wiki/random`, the literal template wins, so adding specific pages beside the catch-all is safe in endpoint routing. With MVC **conventional** routes the docs add a caution: a dedicated conventional route with a catch-all, such as `{*article}`, can be too greedy, so place greedy routes later in the route table. How the ranking works in detail is the business of route precedence. ## Catch-alls in conventional MVC routes Controllers can use a catch-all through a **dedicated conventional route**, one whose template has no `{controller}` or `{action}` and supplies both as defaults: ```csharp app.MapControllerRoute( name: "wiki", pattern: "wiki/{**path}", defaults: new { controller = "Wiki", action = "Page" }); app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); ``` Conventional routes get an order from the sequence of `MapControllerRoute` calls, and the docs recommend placing greedy catch-all routes **after** the more specific ones. Here the wiki route only claims paths under `/wiki`, so its position is harmless; a bare `{**path}` route listed first would be the dangerous case. ## Encoded slashes in the request One more edge: if a client sends `/wiki/fantasy%2Ftolkien`, the server does not decode `%2F` into a separator, so a normal `{slug}` parameter can end up holding a value with `%2F` in it. The routing docs flag that route parameters are not always URL-decoded. If wiki paths can contain characters that need escaping, decode or normalise the captured value deliberately rather than assuming routing did it. ## What interviewers listen for - Both forms capture the remainder of the path and match the same requests. - `*` encodes `/` as `%2F` when generating, `**` keeps it, so `**` round-trips paths. - A catch-all also matches the empty remainder. - It must be last, cannot be optional, and loses to more specific templates.

  • A wiki page endpoint uses {*path} and its generated links look like /wiki/fantasy%2Ftolkien. What is the fix and why does it work?
    Change the template to `{**path}`. Both forms match the same requests, but the double-star form tells the link generator not to encode `/` in the value, so `path = "fantasy/tolkien"` produces `/wiki/fantasy/tolkien`. The single-star form treats the value as one segment and percent-encodes the slash.
  • Does adding /wiki/random beside /wiki/{**path} break either endpoint?
    No. Both match `/wiki/random`, but a literal segment outranks a catch-all, so the specific endpoint wins and every other path under `/wiki/` still reaches the catch-all. In endpoint routing this does not depend on registration order; with conventional MVC routes, keep greedy catch-all routes late in the list.

  • Says {*path} captures one segment while {**path} captures several.
  • Writes {**path?} to let the catch-all match an empty remainder.
  • Puts the catch-all mid-template, as in wiki/{**path}/history.
  • Believes {**path} matches more requests than {*path}.
  • Assumes a /wiki/{**path} Minimal API endpoint shadows a /wiki/random endpoint mapped after it.