Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Components/Endpoints/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#nullable enable
Microsoft.AspNetCore.Components.Endpoints.BasePath
Microsoft.AspNetCore.Components.Endpoints.BasePath.BasePath() -> void
46 changes: 46 additions & 0 deletions src/Components/Endpoints/src/Routing/BasePath.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.Components.Rendering;

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
{
private RenderHandle _renderHandle;

[Inject]
private NavigationManager NavigationManager { get; set; } = default!;

void IComponent.Attach(RenderHandle renderHandle)
{
_renderHandle = renderHandle;
}

Task IComponent.SetParametersAsync(ParameterView parameters)
{
_renderHandle.Render(Render);
return Task.CompletedTask;
}

private void Render(RenderTreeBuilder builder)
{
builder.OpenElement(0, "base");
builder.AddAttribute(1, "href", ComputeHref());
builder.CloseElement();
}

private string ComputeHref()
{
var baseUri = NavigationManager.BaseUri;
if (Uri.TryCreate(baseUri, UriKind.Absolute, out var absoluteUri))
{
return absoluteUri.AbsolutePath;
}

return "/";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="..\..\Shared\test\AutoRenderComponent.cs" Link="TestComponents\AutoRenderComponent.cs" />
<Compile Include="$(ComponentsSharedSourceRoot)src\WebRootComponentParameters.cs" Link="Shared\WebRootComponentParameters.cs" />
<Compile Include="$(ComponentsSharedSourceRoot)test\**\*.cs" LinkBase="Shared" />
</ItemGroup>

<ItemGroup>
Expand Down
93 changes: 93 additions & 0 deletions src/Components/Endpoints/test/Routing/BasePathTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.Components.RenderTree;
using Microsoft.AspNetCore.Components.Test.Helpers;

#nullable enable

namespace Microsoft.AspNetCore.Components.Endpoints;

public class BasePathTest
{
[Fact]
public void PreservesCasingFromNavigationManagerBaseUri()
{
_ = CreateServices(out var renderer, "https://example.com/Dashboard/");
var componentId = RenderBasePath(renderer);

Assert.Equal("/Dashboard/", GetHref(renderer, componentId));
}

[Theory]
[InlineData("https://example.com/a/b/", "/a/b/")]
[InlineData("https://example.com/a/b", "/a/")]
public void RendersBaseUriPathExactly(string baseUri, string expected)
{
_ = CreateServices(out var renderer, baseUri);

var componentId = RenderBasePath(renderer);

Assert.Equal(expected, GetHref(renderer, componentId));
}

private static TestServiceProvider CreateServices(out TestRenderer renderer, string baseUri = "https://example.com/app/")
{
var services = new TestServiceProvider();
var uri = baseUri.EndsWith('/') ? baseUri + "dashboard" : baseUri + "/dashboard";
var navigationManager = new TestNavigationManager(baseUri, uri);
services.AddService<NavigationManager>(navigationManager);
services.AddService<IServiceProvider>(services);

renderer = new TestRenderer(services);
return services;
}

private static int RenderBasePath(TestRenderer renderer)
{
var component = (BasePath)renderer.InstantiateComponent<BasePath>();
var componentId = renderer.AssignRootComponentId(component);
renderer.RenderRootComponent(componentId);
return componentId;
}

private static string? GetHref(TestRenderer renderer, int componentId)
{
var frames = renderer.GetCurrentRenderTreeFrames(componentId);
for (var i = 0; i < frames.Count; i++)
{
ref readonly var frame = ref frames.Array[i];
if (frame.FrameType == RenderTreeFrameType.Element && frame.ElementName == "base")
{
for (var j = i + 1; j < frames.Count; j++)
{
ref readonly var attribute = ref frames.Array[j];
if (attribute.FrameType == RenderTreeFrameType.Attribute && attribute.AttributeName == "href")
{
return attribute.AttributeValue?.ToString();
}

if (attribute.FrameType != RenderTreeFrameType.Attribute)
{
break;
}
}
}
}

return null;
}

private sealed class TestNavigationManager : NavigationManager
{
public TestNavigationManager(string baseUri, string uri)
{
Initialize(baseUri, uri);
}

protected override void NavigateToCore(string uri, bool forceLoad)
{
throw new NotImplementedException();
}
}
}
2 changes: 1 addition & 1 deletion src/Components/Samples/BlazorUnitedApp/App.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<base href="/" />
<BasePath />
<link rel="stylesheet" href="@Assets["css/bootstrap/bootstrap.min.css"]" />
<link rel="stylesheet" href="@Assets["css/bootstrap-icons/bootstrap-icons.min.css"]" />
<link rel="stylesheet" href="@Assets["css/site.css"]" />
Expand Down
1 change: 1 addition & 0 deletions src/Components/Samples/BlazorUnitedApp/_Imports.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.Endpoints
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.JSInterop
@using BlazorUnitedApp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
@using TestContentPackage.NotFound
@using Components.TestServer.RazorComponents
@using Microsoft.AspNetCore.Components
@using Microsoft.AspNetCore.Components.Endpoints
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using System.Threading.Tasks
Expand Down Expand Up @@ -99,7 +100,7 @@
<html lang="en">
<head>
<meta charset="utf-8" />
<base href="/subdir/" />
<BasePath />
<HeadOutlet />
</head>
<body>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
@using Components.TestServer.RazorComponents.Pages.Forms
@using Microsoft.AspNetCore.Components.Endpoints

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<base href="/subdir/" />
<BasePath />
<HeadOutlet />
</head>
<body>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<!DOCTYPE html>
@using Microsoft.AspNetCore.Components.Endpoints

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8" />
<base href="/subdir/" />
<BasePath />

<HeadOutlet @rendermode="new InteractiveWebAssemblyRenderMode(prerender: false)" />
</head>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
@using Components.TestServer.RazorComponents.Pages.Forms
@using Microsoft.AspNetCore.Components.Endpoints
@using Microsoft.AspNetCore.Components.Web

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<base href="/subdir/" />
<BasePath />
<link rel="stylesheet" href="@Assets["Components.TestServer.styles.css"]" />
<HeadOutlet />
</head>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<base href="/" />
<BasePath />
<ResourcePreloader />
@*#if (SampleContent)
<link rel="stylesheet" href="@Assets["lib/bootstrap/dist/css/bootstrap.min.css"]" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@using System.Net.Http
@using System.Net.Http.Json
@using Microsoft.AspNetCore.Components.Endpoints
@*#if (IndividualLocalAuth)
@using Microsoft.AspNetCore.Components.Authorization
##endif*@
Expand Down
Loading