diff --git a/aspnetcore/blazor/host-and-deploy/app-base-path.md b/aspnetcore/blazor/host-and-deploy/app-base-path.md index baf90c56d2a3..6f2eebc3e79a 100644 --- a/aspnetcore/blazor/host-and-deploy/app-base-path.md +++ b/aspnetcore/blazor/host-and-deploy/app-base-path.md @@ -60,19 +60,23 @@ For the sources of links that pertain to Blazor in ASP.NET Core apps: 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. -There are two approaches to deal with the challenge of resolving relative links correctly: +Choose one of the following strategies to keep relative links resolving correctly: -* Map the resources dynamically using the document they were rendered on as the root. -* Set a consistent base path for the document and map the resources under that base path. +* Map resources dynamically using the document they were rendered on as the root. This requires custom routing logic (for example, or a ) and is typically reserved for highly dynamic sites. -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`: +:::moniker range=">= aspnetcore-11.0" -* Rendered under `/Admin/B/C/`, the page is rendered with a path of `/Admin/B/C/Something/Else`. -* Rendered under `/Admin/D/E/`, the page is rendered ***at the same path*** of `/Admin/B/C/Something/Else`. +* 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 `` automatically, while standalone WebAssembly apps (or scenarios that always use a fixed value) configure the literal `` tag in the host page. -Under the first approach, routing offers and , which in combination can be the basis for implementing a completely dynamic solution that determines at runtime about how requests are routed. +:::moniker-end + +:::moniker range="< aspnetcore-11.0" + +* Establish a consistent base path for the document and map all app resources beneath that path. Configure the literal `` tag in the host page for both server-side Blazor apps and Blazor WebAssembly apps (or scenarios that always use a fixed value). + +:::moniker-end -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. +The remainder of this article focuses on implementing a consistent base path. ## Server-side Blazor @@ -126,7 +130,13 @@ By configuring the app base path, a component that isn't in the root directory c Place the `` tag in `` markup ([location of `` 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 `` elements. -:::moniker range=">= aspnetcore-8.0" +:::moniker range=">= aspnetcore-11.0" + +In many hosting scenarios, the relative URL path to the app is the root of the app. When the app runs at `/`, `` renders `` automatically in [`` content](xref:blazor/project-structure#location-of-head-and-body-content). For any other deployment path, the component emits a `` element that matches the current request's path base. + +:::moniker-end + +:::moniker range=">= aspnetcore-8.0 < aspnetcore-11.0" 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 `` in [`` content](xref:blazor/project-structure#location-of-head-and-body-content). @@ -146,6 +156,18 @@ In many hosting scenarios, the relative URL path to the app is the root of the a * In a server-side Blazor app, use ***either*** of the following approaches: +:::moniker range=">= aspnetcore-11.0" + + * Option 1: Use the `BasePath` component to set the app's base path ([location of `` content](xref:blazor/project-structure#location-of-head-and-body-content)): + + ```razor + + ``` + + The component renders `` automatically based on the current request path base. + +:::moniker range="< aspnetcore-11.0" + * Option 1: Use the `` tag to set the app's base path ([location of `` content](xref:blazor/project-structure#location-of-head-and-body-content)): ```html @@ -154,6 +176,8 @@ In many hosting scenarios, the relative URL path to the app is the root of the a **The trailing slash is required.** +:::moniker-end + * Option 2: Call ***first*** in the app's request processing pipeline (`Program.cs`) immediately after the is built (`builder.Build()`) to configure the base path for any following middleware that interacts with the request path: ```csharp