From 0bf146004cacf9caeae413f569ceb194b11d837a Mon Sep 17 00:00:00 2001 From: Luke Latham <1622880+guardrex@users.noreply.github.com> Date: Wed, 31 Jul 2024 08:33:21 -0400 Subject: [PATCH] Add section on avoiding file capture (#33212) --- aspnetcore/blazor/fundamentals/routing.md | 22 +++++++++++++++++++ .../blazor/fundamentals/static-files.md | 4 +++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/aspnetcore/blazor/fundamentals/routing.md b/aspnetcore/blazor/fundamentals/routing.md index f5bb365a33..4ea09ca4a9 100644 --- a/aspnetcore/blazor/fundamentals/routing.md +++ b/aspnetcore/blazor/fundamentals/routing.md @@ -357,6 +357,7 @@ Constraint | Example | Example Matches | Invariant
culture
matching `guid` | `{id:guid}` | `CD2C1638-1638-72D5-1638-DEADBEEF1638`, `{CD2C1638-1638-72D5-1638-DEADBEEF1638}` | No `int` | `{id:int}` | `123456789`, `-123456789` | Yes `long` | `{ticks:long}` | `123456789`, `-123456789` | Yes +`nonfile` | `{parameter:nonfile}` | Not `BlazorSample.styles.css`, not `favicon.ico` | Yes > [!WARNING] > Route constraints that verify the URL and are converted to a CLR type (such as `int` or ) always use the invariant culture. These constraints assume that the URL is non-localizable. @@ -389,6 +390,27 @@ Route constraints also work with [optional parameters](#route-parameters). In th :::moniker-end +## Avoid file capture in a route parameter + +The following route template inadvertently captures static asset paths in its optional route parameter (`Optional`). For example, the app's stylesheet (`.styles.css`) is captured, which breaks the app's styles: + +```razor +@page "/{optional?}" + +... + +@code { + [Parameter] + public string? Optional { get; set; } +} +``` + +To restrict a route parameter to capturing non-file paths, use the [`:nonfile` constraint](#route-constraints) in the route template: + +```razor +@page "/{optional:nonfile?}" +``` + :::moniker range="< aspnetcore-8.0"