Skip to content

Conversation

@ilonatommy
Copy link
Member

@ilonatommy ilonatommy commented Dec 2, 2025

Summary

Proposal addressing #6818 by introducing a reusable BasePath component that centralizes how Blazor apps compute their <base> URI when hosted under subpaths.

Background

Routing case preservation for apps mounted at /dashboard or 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 gives Blazor Web a single, framework-owned solution while standalone WebAssembly apps 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

namespace Microsoft.AspNetCore.Components.Endpoints;

/// <summary>
/// Renders a &lt;base&gt; element whose <c>href</c> value matches the current request path base.
/// </summary>
public sealed class BasePath : IComponent
{
    // No public parameters; renders based on NavigationManager.BaseUri at runtime.
}

Implementation Details

  • BasePath implements IComponent, renders a <base> element via RenderHandle, and resolves the href from NavigationManager.BaseUri.
  • Logic falls back to / if the base URI cannot be parsed, but otherwise mirrors both casing and multi-segment paths exposed by the endpoint pipeline.
  • Emits the exact path reported by NavigationManager.
  • The class lives in src/Components/Endpoints/src/Routing/BasePath.cs and 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

@using Microsoft.AspNetCore.Components.Endpoints

<!-- Blazor Web layout -->
<head>
    <BasePath />
    <HeadOutlet />
</head>

Fixes #6818.

@ilonatommy ilonatommy added this to the .NET 11 Planning milestone Dec 2, 2025
@ilonatommy ilonatommy self-assigned this Dec 2, 2025
Copilot AI review requested due to automatic review settings December 2, 2025 10:15
@ilonatommy ilonatommy added the area-blazor Includes: Blazor, Razor Components label Dec 2, 2025
@ilonatommy ilonatommy requested a review from a team as a code owner December 2, 2025 10:15
Copilot finished reviewing on behalf of ilonatommy December 2, 2025 10:18
Copy link
Contributor

Copilot AI left a 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 BasePath component with automatic path base detection from IHttpContextAccessor, NavigationManager, and fallback options
  • Introduced a new dependency on Microsoft.AspNetCore.Http.Abstractions for accessing request context
  • Exposed new public API surface including the BasePath class and its Href and FallbackHref properties

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

Copy link
Member

@javiercn javiercn left a 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.

@ilonatommy ilonatommy enabled auto-merge (squash) December 3, 2025 14:10
@ilonatommy ilonatommy merged commit c8d47d3 into dotnet:main Dec 3, 2025
30 checks passed
@dotnet-policy-service dotnet-policy-service bot removed this from the .NET 11 Planning milestone Dec 3, 2025
@dotnet-policy-service dotnet-policy-service bot added this to the 11.0-preview1 milestone Dec 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Blazor has case sensitive URLs when hosted in a Child Directory

2 participants