Skip to content

Commit a86514f

Browse files
ArgoZhangkid5791DejanHuang
authored
fix(Layout): authorization failure in virtual directory mode (#6086)
* feat: 增加扩展方法 * fix: 修复二级虚拟目录 Layout 无法显示问题 * chore: bump version 9.6.5-beta01 Co-Authored-By: kid5791 <[email protected]> * chore: bump version 9.6.5-beta02 Co-Authored-By: dejan <[email protected]> * test: 增加单元测试 * refactor: 更改方法名称 * test: 更新单元测试 --------- Co-authored-by: kid5791 <[email protected]> Co-authored-by: dejan <[email protected]>
1 parent 2b7554a commit a86514f

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.6.4</Version>
4+
<Version>9.6.5-beta01</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/Layout/Layout.razor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,8 @@ protected override async Task OnInitializedAsync()
500500
// wasm 模式下 开启权限必须提供 AdditionalAssemblies 参数
501501
AdditionalAssemblies ??= [Assembly.GetEntryAssembly()!];
502502

503-
var uri = Navigation.ToAbsoluteUri(Navigation.Uri);
504-
var context = RouteTableFactory.Create(AdditionalAssemblies, uri.LocalPath);
503+
var url = Navigation.ToBaseRelativePathWithoutQueryAndFragment();
504+
var context = RouteTableFactory.Create(AdditionalAssemblies, url);
505505
if (context.Handler != null)
506506
{
507507
IsAuthenticated = await context.Handler.IsAuthorizedAsync(ServiceProvider, AuthenticationStateTask, Resource);

src/BootstrapBlazor/Extensions/NavigationManagerExtensions.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,27 @@ public static void NavigateTo(this NavigationManager navigation, IServiceProvide
2929
option.Closable = closable;
3030
navigation.NavigateTo(url);
3131
}
32+
33+
/// <summary>
34+
/// 获得当前 Url 的相对路径,不包含 QueryString 和 Fragment(Hash)
35+
/// </summary>
36+
/// <param name="navigationManager"></param>
37+
/// <returns></returns>
38+
public static string ToBaseRelativePathWithoutQueryAndFragment(this NavigationManager navigationManager)
39+
{
40+
var url = navigationManager.ToBaseRelativePath(navigationManager.Uri);
41+
42+
var index = url.IndexOf('?');
43+
if (index > -1)
44+
{
45+
url = url[..index];
46+
}
47+
48+
index = url.IndexOf('#');
49+
if (index > -1)
50+
{
51+
url = url[..index];
52+
}
53+
return url;
54+
}
3255
}

test/UnitTest/Extensions/NavigationManagerExtensionsTest.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,15 @@ public void NavigateTo_Ok()
1616
var provider = Context.Services.GetRequiredService<IServiceProvider>();
1717
nav.NavigateTo(provider, "/Cat", "Cat", "fa-solid fa-font-awesome", false);
1818
}
19+
20+
[Fact]
21+
public void ToBaseRelativePathWithoutQueryAndFragment_Ok()
22+
{
23+
var nav = Context.Services.GetRequiredService<FakeNavigationManager>();
24+
nav.NavigateTo("/test?test1=1");
25+
Assert.Equal("test", nav.ToBaseRelativePathWithoutQueryAndFragment());
26+
27+
nav.NavigateTo("/test#1234");
28+
Assert.Equal("test", nav.ToBaseRelativePathWithoutQueryAndFragment());
29+
}
1930
}

0 commit comments

Comments
 (0)