-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Blazor Web can use BasePath component instead of <base href="">
#64590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR introduces a new BasePath component that allows Blazor Web applications to dynamically set the <base href=""> element based on the current request path base, providing an alternative to hardcoded base tags. The component intelligently determines the base path from multiple sources with a clear precedence order.
Key changes:
- Added
BasePathcomponent with automatic path base detection fromIHttpContextAccessor,NavigationManager, and fallback options - Introduced a new dependency on
Microsoft.AspNetCore.Http.Abstractionsfor accessing request context - Exposed new public API surface including the
BasePathclass and itsHrefandFallbackHrefproperties
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/Components/Web/src/Head/BasePath.cs | New sealed component that renders a <base> element with href computed from request PathBase, explicit parameters, or NavigationManager |
| src/Components/Web/src/PublicAPI.Unshipped.txt | Adds public API entries for the new BasePath component and its properties |
| src/Components/Web/src/Microsoft.AspNetCore.Components.Web.csproj | Adds reference to Microsoft.AspNetCore.Http.Abstractions for IHttpContextAccessor support |
src/Components/Web/test/Microsoft.AspNetCore.Components.Web.Tests.csproj
Outdated
Show resolved
Hide resolved
src/Components/Web/src/Microsoft.AspNetCore.Components.Web.csproj
Outdated
Show resolved
Hide resolved
src/Components/Endpoints/test/Microsoft.AspNetCore.Components.Endpoints.Tests.csproj
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great.
I think we want to update our template with this too. One more thing is you want to give the component more coverage would be to replace the base tag on our App.razor files with this code.
Summary
Proposal addressing #6818 by introducing a reusable
BasePathcomponent that centralizes how Blazor apps compute their<base>URI when hosted under subpaths.Background
Routing case preservation for apps mounted at
/dashboardor similar previously required manual<base href>edits or per-app JavaScript. Those workarounds were inconsistent across hosting models and easy to break during deployment. Moving the logic into the Endpoints package givesBlazor Weba single, framework-owned solution while standaloneWebAssemblyapps keep using their static<base />.This gives developers a first-class, framework-supported answer instead of per-app workarounds mentioned throughout #6818.
Changes
Public API
Implementation Details
BasePathimplementsIComponent, renders a<base>element viaRenderHandle, and resolves the href fromNavigationManager.BaseUri./if the base URI cannot be parsed, but otherwise mirrors both casing and multi-segment paths exposed by the endpoint pipeline.NavigationManager.src/Components/Endpoints/src/Routing/BasePath.csand will share the directory with future routing-related document helpers.Tests
The component is covered with unit tests and passed manual testing. Adding e2e test seems too heavy for this kind of change.
API usage
Fixes #6818.