All 15 Endpoint Metadata, Route Constraints & Templates questions
More in Endpoint Metadata, Route Constraints & Templates
Advertisement
ASP.NET Core › Endpoint Metadata, Route Constraints & Templates

How do complex/composite route segments (combining literal text and a parameter in the same segment, like {fileName}.{extension}) work?

Intermediate

Answer

A single URL segment can combine literal characters and multiple parameters, like {fileName}.{extension}, letting the routing system parse a segment like 'report.pdf' into fileName='report' and extension='pdf' -- though this pattern requires unambiguous delimiters (like the literal dot here) since the router needs to determine where one parameter ends and the next literal/parameter begins.
app.MapGet("/files/{fileName}.{extension}", (string fileName, string extension) => {
    // GET /files/report.pdf -> fileName = "report", extension = "pdf"
    return DownloadFile(fileName, extension);
});
Real-world example A file-serving endpoint splits a single URL segment like 'report.pdf' into separate fileName and extension route values using a composite route segment, letting handler code work with each part independently without manual string splitting.

Common follow-ups: What ambiguity issues can arise with composite segments lacking a clear literal delimiter?;How does this compare to just accepting the full segment as one string and splitting it manually in the handler?