diff --git a/framework/Volo.Abp.slnx b/framework/Volo.Abp.slnx
index 73686e5dbe..526bfebe0d 100644
--- a/framework/Volo.Abp.slnx
+++ b/framework/Volo.Abp.slnx
@@ -188,6 +188,7 @@
+
diff --git a/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor/Bundling/AbpScripts.razor b/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor/Bundling/AbpScripts.razor
index 436a22fc0e..6560d97bd6 100644
--- a/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor/Bundling/AbpScripts.razor
+++ b/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor/Bundling/AbpScripts.razor
@@ -1,16 +1,14 @@
+@using Microsoft.AspNetCore.Components
@implements IDisposable
@inject IComponentBundleManager BundleManager
@inject PersistentComponentState ApplicationState
+@inject NavigationManager NavigationManager
+@inject IComponentBundleUrlBuilder BundleUrlBuilder
@if (ScriptFiles != null)
{
foreach (var file in ScriptFiles)
{
- var src = file;
- if (!AppBasePath.IsNullOrWhiteSpace())
- {
- src = AppBasePath.EnsureEndsWith('/') + file.RemovePreFix("/");
- }
-
+
}
}
@@ -39,18 +37,29 @@
// We are in prerendering mode
if (!BundleName.IsNullOrWhiteSpace())
{
- ScriptFiles = (await BundleManager.GetScriptBundleFilesAsync(BundleName!)).ToList();
+ var rawFiles = await BundleManager.GetScriptBundleFilesAsync(BundleName!);
+ ScriptFiles = await ResolveAsync(rawFiles);
}
}
else
{
if (OperatingSystem.IsBrowser() && WebAssemblyScriptFiles != null)
{
- ScriptFiles = WebAssemblyScriptFiles;
+ ScriptFiles = await ResolveAsync(WebAssemblyScriptFiles);
}
}
}
+ private async Task> ResolveAsync(IReadOnlyList files)
+ {
+ var resolved = new List(files.Count);
+ foreach (var file in files)
+ {
+ resolved.Add(await BundleUrlBuilder.BuildAsync(file, AppBasePath, NavigationManager.BaseUri));
+ }
+ return resolved;
+ }
+
private Task Callback()
{
ApplicationState.PersistAsJson(PrerenderedKey, PrerenderedKey);
diff --git a/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor/Bundling/AbpStyles.razor b/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor/Bundling/AbpStyles.razor
index 1c2ede488f..015ef9fa35 100644
--- a/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor/Bundling/AbpStyles.razor
+++ b/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor/Bundling/AbpStyles.razor
@@ -1,16 +1,14 @@
+@using Microsoft.AspNetCore.Components
@implements IDisposable
@inject IComponentBundleManager BundleManager
@inject PersistentComponentState ApplicationState
+@inject NavigationManager NavigationManager
+@inject IComponentBundleUrlBuilder BundleUrlBuilder
@if (StyleFiles != null)
{
foreach (var file in StyleFiles)
{
- var href = file;
- if (!AppBasePath.IsNullOrWhiteSpace())
- {
- href = AppBasePath.EnsureEndsWith('/') + file.RemovePreFix("/");
- }
-
+
}
}
@@ -39,7 +37,8 @@
// We are in prerendering mode
if (!BundleName.IsNullOrWhiteSpace())
{
- StyleFiles = (await BundleManager.GetStyleBundleFilesAsync(BundleName!)).ToList();
+ var rawFiles = await BundleManager.GetStyleBundleFilesAsync(BundleName!);
+ StyleFiles = await ResolveAsync(rawFiles);
}
}
else
@@ -47,7 +46,7 @@
StyleFiles = scriptFiles;
if (OperatingSystem.IsBrowser() && StyleFiles != null && WebAssemblyStyleFiles != null)
{
- StyleFiles.AddIfNotContains(WebAssemblyStyleFiles);
+ StyleFiles.AddIfNotContains(await ResolveAsync(WebAssemblyStyleFiles));
}
}
}
@@ -60,11 +59,21 @@
{
_hasRemoveServerStyle = true;
await Task.Delay(3000);
- StyleFiles = WebAssemblyStyleFiles;
+ StyleFiles = await ResolveAsync(WebAssemblyStyleFiles);
StateHasChanged();
}
}
+ private async Task> ResolveAsync(IReadOnlyList files)
+ {
+ var resolved = new List(files.Count);
+ foreach (var file in files)
+ {
+ resolved.Add(await BundleUrlBuilder.BuildAsync(file, AppBasePath, NavigationManager.BaseUri));
+ }
+ return resolved;
+ }
+
private Task Callback()
{
ApplicationState.PersistAsJson(PrerenderedKey, StyleFiles);
diff --git a/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor/Bundling/ComponentBundleUrlBuilder.cs b/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor/Bundling/ComponentBundleUrlBuilder.cs
new file mode 100644
index 0000000000..f813f90d8a
--- /dev/null
+++ b/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor/Bundling/ComponentBundleUrlBuilder.cs
@@ -0,0 +1,62 @@
+using System;
+using System.Threading.Tasks;
+using JetBrains.Annotations;
+using Volo.Abp.DependencyInjection;
+
+namespace Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor.Bundling;
+
+public class ComponentBundleUrlBuilder : IComponentBundleUrlBuilder, ITransientDependency
+{
+ public virtual Task BuildAsync(
+ [NotNull] string fileName,
+ [CanBeNull] string? appBasePath,
+ [CanBeNull] string? navigationBaseUri)
+ {
+ Check.NotNull(fileName, nameof(fileName));
+
+ if (IsExternalUrl(fileName))
+ {
+ return Task.FromResult(fileName);
+ }
+
+ var pathBase = !string.IsNullOrWhiteSpace(appBasePath)
+ ? appBasePath
+ : ExtractPathBaseFromNavigationBaseUri(navigationBaseUri);
+
+ if (string.IsNullOrWhiteSpace(pathBase))
+ {
+ return Task.FromResult(fileName);
+ }
+
+ var normalized = pathBase.EnsureEndsWith('/');
+ if (normalized == "/")
+ {
+ return Task.FromResult(fileName);
+ }
+
+ if (fileName.StartsWith(normalized, StringComparison.Ordinal))
+ {
+ return Task.FromResult(fileName);
+ }
+
+ return Task.FromResult(normalized + fileName.RemovePreFix("/"));
+ }
+
+ protected virtual bool IsExternalUrl([NotNull] string fileName)
+ {
+ return fileName.StartsWith("//", StringComparison.Ordinal) ||
+ (fileName.Contains(':') && Uri.TryCreate(fileName, UriKind.Absolute, out _));
+ }
+
+ protected virtual string? ExtractPathBaseFromNavigationBaseUri([CanBeNull] string? navigationBaseUri)
+ {
+ if (string.IsNullOrWhiteSpace(navigationBaseUri))
+ {
+ return null;
+ }
+
+ return Uri.TryCreate(navigationBaseUri, UriKind.Absolute, out var uri)
+ ? uri.AbsolutePath
+ : null;
+ }
+}
diff --git a/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor/Bundling/IComponentBundleUrlBuilder.cs b/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor/Bundling/IComponentBundleUrlBuilder.cs
new file mode 100644
index 0000000000..633ec3561e
--- /dev/null
+++ b/framework/src/Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor/Bundling/IComponentBundleUrlBuilder.cs
@@ -0,0 +1,8 @@
+using System.Threading.Tasks;
+
+namespace Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor.Bundling;
+
+public interface IComponentBundleUrlBuilder
+{
+ Task BuildAsync(string fileName, string? appBasePath, string? navigationBaseUri);
+}
diff --git a/framework/test/Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor.Tests/Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor.Tests.abppkg b/framework/test/Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor.Tests/Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor.Tests.abppkg
new file mode 100644
index 0000000000..64c1552e37
--- /dev/null
+++ b/framework/test/Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor.Tests/Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor.Tests.abppkg
@@ -0,0 +1,3 @@
+{
+ "role": "lib.test"
+}
diff --git a/framework/test/Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor.Tests/Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor.Tests.csproj b/framework/test/Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor.Tests/Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor.Tests.csproj
new file mode 100644
index 0000000000..5b5ee16623
--- /dev/null
+++ b/framework/test/Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor.Tests/Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor.Tests.csproj
@@ -0,0 +1,17 @@
+
+
+
+
+
+ net10.0
+
+ enable
+
+
+
+
+
+
+
+
+
diff --git a/framework/test/Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor.Tests/Volo/Abp/AspNetCore/Components/Web/Theming/MudBlazor/Bundling/ComponentBundleUrlBuilder_Tests.cs b/framework/test/Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor.Tests/Volo/Abp/AspNetCore/Components/Web/Theming/MudBlazor/Bundling/ComponentBundleUrlBuilder_Tests.cs
new file mode 100644
index 0000000000..8df0f04b9d
--- /dev/null
+++ b/framework/test/Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor.Tests/Volo/Abp/AspNetCore/Components/Web/Theming/MudBlazor/Bundling/ComponentBundleUrlBuilder_Tests.cs
@@ -0,0 +1,119 @@
+using System.Threading.Tasks;
+using Shouldly;
+using Xunit;
+
+namespace Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor.Bundling;
+
+public class ComponentBundleUrlBuilder_Tests
+{
+ private readonly IComponentBundleUrlBuilder _builder = new ComponentBundleUrlBuilder();
+
+ [Fact]
+ public async Task Should_Return_FileName_When_No_PathBase_Available()
+ {
+ (await _builder.BuildAsync("/__bundles/Global.css", appBasePath: null, navigationBaseUri: null))
+ .ShouldBe("/__bundles/Global.css");
+ }
+
+ [Fact]
+ public async Task Should_Return_FileName_When_NavigationBaseUri_Has_Root_PathBase()
+ {
+ (await _builder.BuildAsync("/__bundles/Global.css", appBasePath: null, navigationBaseUri: "https://localhost/"))
+ .ShouldBe("/__bundles/Global.css");
+ }
+
+ [Fact]
+ public async Task Should_Use_Explicit_AppBasePath_Over_NavigationBaseUri()
+ {
+ (await _builder.BuildAsync("/__bundles/Global.css", appBasePath: "/explicit", navigationBaseUri: "https://localhost/from-nav/"))
+ .ShouldBe("/explicit/__bundles/Global.css");
+ }
+
+ [Fact]
+ public async Task Should_Resolve_PathBase_From_NavigationBaseUri()
+ {
+ (await _builder.BuildAsync("/__bundles/Global.css", appBasePath: null, navigationBaseUri: "https://localhost/foo/"))
+ .ShouldBe("/foo/__bundles/Global.css");
+ }
+
+ [Fact]
+ public async Task Should_Resolve_PathBase_From_NavigationBaseUri_When_AppBasePath_Empty()
+ {
+ (await _builder.BuildAsync("/__bundles/Global.css", appBasePath: "", navigationBaseUri: "https://localhost/foo/"))
+ .ShouldBe("/foo/__bundles/Global.css");
+ }
+
+ [Fact]
+ public async Task Should_Handle_Nested_PathBase()
+ {
+ (await _builder.BuildAsync("/__bundles/Global.css", appBasePath: null, navigationBaseUri: "https://localhost/foo/bar/"))
+ .ShouldBe("/foo/bar/__bundles/Global.css");
+ }
+
+ [Fact]
+ public async Task Should_Normalize_AppBasePath_Without_Trailing_Slash()
+ {
+ (await _builder.BuildAsync("/__bundles/Global.css", appBasePath: "/foo", navigationBaseUri: null))
+ .ShouldBe("/foo/__bundles/Global.css");
+ }
+
+ [Fact]
+ public async Task Should_Not_Duplicate_Leading_Slash()
+ {
+ (await _builder.BuildAsync("/__bundles/Global.css", appBasePath: "/foo/", navigationBaseUri: null))
+ .ShouldBe("/foo/__bundles/Global.css");
+ }
+
+ [Fact]
+ public async Task Should_Handle_FileName_Without_Leading_Slash()
+ {
+ (await _builder.BuildAsync("__bundles/Global.css", appBasePath: "/foo", navigationBaseUri: null))
+ .ShouldBe("/foo/__bundles/Global.css");
+ }
+
+ [Fact]
+ public async Task Should_Return_FileName_When_NavigationBaseUri_Is_Invalid()
+ {
+ (await _builder.BuildAsync("/__bundles/Global.css", appBasePath: null, navigationBaseUri: "not-a-uri"))
+ .ShouldBe("/__bundles/Global.css");
+ }
+
+ [Fact]
+ public async Task Should_Throw_When_FileName_Is_Null()
+ {
+ await Should.ThrowAsync(async () =>
+ await _builder.BuildAsync(null!, appBasePath: "/foo", navigationBaseUri: null));
+ }
+
+ [Theory]
+ [InlineData("https://cdn.example.com/foo.css")]
+ [InlineData("http://cdn.example.com/foo.css")]
+ [InlineData("//cdn.example.com/foo.css")]
+ [InlineData("data:text/css;base64,Zm9vIA==")]
+ public async Task Should_Not_Prefix_External_Urls(string externalUrl)
+ {
+ (await _builder.BuildAsync(externalUrl, appBasePath: "/foo", navigationBaseUri: "https://localhost/foo/"))
+ .ShouldBe(externalUrl);
+ }
+
+ [Fact]
+ public async Task Should_Treat_Whitespace_AppBasePath_As_Not_Provided()
+ {
+ (await _builder.BuildAsync("/__bundles/Global.css", appBasePath: " ", navigationBaseUri: "https://localhost/foo/"))
+ .ShouldBe("/foo/__bundles/Global.css");
+ }
+
+ [Fact]
+ public async Task Should_Be_Idempotent_For_Already_Prefixed_FileName()
+ {
+ (await _builder.BuildAsync("/foo/__bundles/Global.css", appBasePath: "/foo", navigationBaseUri: null))
+ .ShouldBe("/foo/__bundles/Global.css");
+ }
+
+ [Fact]
+ public async Task Should_Be_Idempotent_When_PathBase_Resolved_From_NavigationBaseUri()
+ {
+ (await _builder.BuildAsync("/foo/__bundles/Global.css", appBasePath: null, navigationBaseUri: "https://localhost/foo/"))
+ .ShouldBe("/foo/__bundles/Global.css");
+ }
+}