Skip to content

Commit 80a945e

Browse files
authored
[11.0 P1] Blazor Web can use BasePath component instead of <base href=""> (#36428)
1 parent b345333 commit 80a945e

1 file changed

Lines changed: 37 additions & 11 deletions

File tree

aspnetcore/blazor/host-and-deploy/app-base-path.md

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,23 @@ For the sources of links that pertain to Blazor in ASP.NET Core apps:
6060

6161
If you're rendering a Blazor app from different documents (for example, `/Admin/B/C/` and `/Admin/D/E/`), you must take the app base path into account, or the base path is different when the app renders in each document and the resources are fetched from the wrong URLs.
6262

63-
There are two approaches to deal with the challenge of resolving relative links correctly:
63+
Choose one of the following strategies to keep relative links resolving correctly:
6464

65-
* Map the resources dynamically using the document they were rendered on as the root.
66-
* Set a consistent base path for the document and map the resources under that base path.
65+
* Map resources dynamically using the document they were rendered on as the root. This requires custom routing logic (for example, <xref:Microsoft.AspNetCore.Routing.IDynamicEndpointMetadata> or a <xref:Microsoft.AspNetCore.Routing.MatcherPolicy>) and is typically reserved for highly dynamic sites.
6766

68-
The first option is more complicated and isn't the most typical approach, as it makes navigation different for each document. Consider the following example for rendering a page `/Something/Else`:
67+
:::moniker range=">= aspnetcore-11.0"
6968

70-
* Rendered under `/Admin/B/C/`, the page is rendered with a path of `/Admin/B/C/Something/Else`.
71-
* Rendered under `/Admin/D/E/`, the page is rendered ***at the same path*** of `/Admin/B/C/Something/Else`.
69+
* Establish a consistent base path for the document and map all app resources beneath that path. Blazor Web Apps can rely on the `BasePath` component to render `<base href>` automatically, while standalone WebAssembly apps (or scenarios that always use a fixed value) configure the literal `<base>` tag in the host page.
7270

73-
Under the first approach, routing offers <xref:Microsoft.AspNetCore.Routing.IDynamicEndpointMetadata> and <xref:Microsoft.AspNetCore.Routing.MatcherPolicy>, which in combination can be the basis for implementing a completely dynamic solution that determines at runtime about how requests are routed.
71+
:::moniker-end
72+
73+
:::moniker range="< aspnetcore-11.0"
74+
75+
* Establish a consistent base path for the document and map all app resources beneath that path. Configure the literal `<base>` tag in the host page for both server-side Blazor apps and Blazor WebAssembly apps (or scenarios that always use a fixed value).
76+
77+
:::moniker-end
7478

75-
For the second option, which is the usual approach taken, the app sets the base path in the document and maps the server endpoints to paths under the base. The following guidance adopts this approach.
79+
The remainder of this article focuses on implementing a consistent base path.
7680

7781
## Server-side Blazor
7882

@@ -135,7 +139,13 @@ By configuring the app base path, a component that isn't in the root directory c
135139

136140
Place the `<base>` tag in `<head>` markup ([location of `<head>` content](xref:blazor/project-structure#location-of-head-and-body-content)) before any elements with attribute values that are URLs, such as the `href` attributes of `<link>` elements.
137141

138-
:::moniker range=">= aspnetcore-8.0"
142+
:::moniker range=">= aspnetcore-11.0"
143+
144+
In many hosting scenarios, the relative URL path to the app is the root of the app. When the app runs at `/`, `<BasePath />` renders `<base href="/" />` automatically in [`<head>` content](xref:blazor/project-structure#location-of-head-and-body-content). For any other deployment path, the component emits a `<base>` element that matches the current request's path base.
145+
146+
:::moniker-end
147+
148+
:::moniker range=">= aspnetcore-8.0 < aspnetcore-11.0"
139149

140150
In many hosting scenarios, the relative URL path to the app is the root of the app. In these default cases, the app's relative URL base path is `/` configured as `<base href="/" />` in [`<head>` content](xref:blazor/project-structure#location-of-head-and-body-content).
141151

@@ -155,15 +165,31 @@ In many hosting scenarios, the relative URL path to the app is the root of the a
155165
156166
* In a server-side Blazor app, use the following approach:
157167

158-
* Use the `<base>` tag to set the app's base path ([location of `<head>` content](xref:blazor/project-structure#location-of-head-and-body-content)):
168+
:::moniker range=">= aspnetcore-11.0"
169+
170+
* Option 1: Use the `BasePath` component to set the app's base path ([location of `<head>` content](xref:blazor/project-structure#location-of-head-and-body-content)):
171+
172+
```razor
173+
<BasePath />
174+
```
175+
176+
The component renders `<base href>` automatically based on the current request path base.
177+
178+
::: moniker-end
179+
180+
:::moniker range="< aspnetcore-11.0"
181+
182+
* Option 1: Use the `<base>` tag to set the app's base path ([location of `<head>` content](xref:blazor/project-structure#location-of-head-and-body-content)):
159183
160184
```html
161185
<base href="/CoolApp/">
162186
```
163187
164188
**The trailing slash is required.**
165189
166-
* Call <xref:Microsoft.AspNetCore.Builder.UsePathBaseExtensions.UsePathBase%2A> ***first*** in the app's request processing pipeline (`Program.cs`) immediately after the <xref:Microsoft.AspNetCore.Builder.WebApplicationBuilder> is built (`builder.Build()`) to configure the base path for any following middleware that interacts with the request path:
190+
:::moniker-end
191+
192+
* Option 2: Call <xref:Microsoft.AspNetCore.Builder.UsePathBaseExtensions.UsePathBase%2A> ***first*** in the app's request processing pipeline (`Program.cs`) immediately after the <xref:Microsoft.AspNetCore.Builder.WebApplicationBuilder> is built (`builder.Build()`) to configure the base path for any following middleware that interacts with the request path:
167193
168194
```csharp
169195
app.UsePathBase("/CoolApp");

0 commit comments

Comments
 (0)