-
Notifications
You must be signed in to change notification settings - Fork 10.6k
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
50a18ea
Proposal of `BasePath` component.
ilonatommy 237ed87
Unit tests.
ilonatommy 054357f
Apply feedback: richer doc comment + refactoring + more test cases.
ilonatommy 9a57bc3
Feedback: clean the code and tests.
ilonatommy 2703bee
Feedback: change base class.
ilonatommy 282d927
Move the component to the server project.
ilonatommy 1b5c785
Fix namespace.
ilonatommy 6bb7d04
Avoid duplicates.
ilonatommy b1b1a03
Feedback: give the component more coverage.
ilonatommy fcdc870
Feedback: update our template.
ilonatommy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 <base> 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 "/"; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...t/testassets/Components.TestServer/RazorComponents/NamedFormContextNoFormContextApp.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 4 additions & 2 deletions
6
...nents/test/testassets/Components.TestServer/RazorComponents/RemoteAuthenticationApp.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
src/Components/test/testassets/Components.TestServer/RazorComponents/Root.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWebCSharp.1/Components/_Imports.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.