Skip to content

Commit 0bf1460

Browse files
authored
Add section on avoiding file capture (#33212)
1 parent ded1200 commit 0bf1460

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

aspnetcore/blazor/fundamentals/routing.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ Constraint | Example | Example Matches | Invariant<br>culture<br>matching
357357
`guid` | `{id:guid}` | `CD2C1638-1638-72D5-1638-DEADBEEF1638`, `{CD2C1638-1638-72D5-1638-DEADBEEF1638}` | No
358358
`int` | `{id:int}` | `123456789`, `-123456789` | Yes
359359
`long` | `{ticks:long}` | `123456789`, `-123456789` | Yes
360+
`nonfile` | `{parameter:nonfile}` | Not `BlazorSample.styles.css`, not `favicon.ico` | Yes
360361

361362
> [!WARNING]
362363
> Route constraints that verify the URL and are converted to a CLR type (such as `int` or <xref:System.DateTime>) 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
389390

390391
:::moniker-end
391392

393+
## Avoid file capture in a route parameter
394+
395+
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:
396+
397+
```razor
398+
@page "/{optional?}"
399+
400+
...
401+
402+
@code {
403+
[Parameter]
404+
public string? Optional { get; set; }
405+
}
406+
```
407+
408+
To restrict a route parameter to capturing non-file paths, use the [`:nonfile` constraint](#route-constraints) in the route template:
409+
410+
```razor
411+
@page "/{optional:nonfile?}"
412+
```
413+
392414
:::moniker range="< aspnetcore-8.0"
393415

394416
<!--

aspnetcore/blazor/fundamentals/static-files.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,13 +526,15 @@ Run the app.
526526

527527
:::moniker range=">= aspnetcore-8.0"
528528

529-
[App base path](xref:blazor/host-and-deploy/index#app-base-path)
529+
* [App base path](xref:blazor/host-and-deploy/index#app-base-path)
530+
* [Avoid file capture in a route parameter](xref:blazor/fundamentals/routing#avoid-file-capture-in-a-route-parameter)
530531

531532
:::moniker-end
532533

533534
:::moniker range="< aspnetcore-8.0"
534535

535536
* [App base path](xref:blazor/host-and-deploy/index#app-base-path)
537+
* [Avoid file capture in a route parameter](xref:blazor/fundamentals/routing#avoid-file-capture-in-a-route-parameter)
536538
* <xref:blazor/host-and-deploy/multiple-hosted-webassembly>
537539

538540
:::moniker-end

0 commit comments

Comments
 (0)