diff --git a/framework/Volo.Abp.slnx b/framework/Volo.Abp.slnx index 133fd89eed..07068df7c6 100644 --- a/framework/Volo.Abp.slnx +++ b/framework/Volo.Abp.slnx @@ -189,6 +189,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/src/Volo.Abp.AspNetCore.Components.Web/Volo/Abp/AspNetCore/Components/Web/MenuItemUrlHelper.cs b/framework/src/Volo.Abp.AspNetCore.Components.Web/Volo/Abp/AspNetCore/Components/Web/MenuItemUrlHelper.cs new file mode 100644 index 0000000000..c69024b9cd --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Components.Web/Volo/Abp/AspNetCore/Components/Web/MenuItemUrlHelper.cs @@ -0,0 +1,20 @@ +using System; + +namespace Volo.Abp.AspNetCore.Components.Web; + +public static class MenuItemUrlHelper +{ + /// + /// Makes an app-local menu url ("/x" or "~/x") relative to the base href, so it keeps the PathBase. + /// Protocol-relative urls ("//host/x") point to another host and are returned as is. + /// + public static string? ToBaseRelativeUrl(string? url) + { + if (url == null || url.StartsWith("//", StringComparison.Ordinal)) + { + return url; + } + + return url.TrimStart('/', '~'); + } +} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperResourceService.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperResourceService.cs index b83d090f11..91618566ac 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperResourceService.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperResourceService.cs @@ -5,7 +5,9 @@ using System.Collections.Generic; using System.Diagnostics; using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.Rendering; +using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; @@ -100,6 +102,35 @@ public abstract class AbpTagHelperResourceService : ITransientDependency output.Content.AppendHtml($"{Environment.NewLine}"); } + /// + /// External files are not bundled, but an external file given as a local path ("/x" or "~/x") + /// is still served by this application, so it needs the request's PathBase like the other files. + /// A "/x" path that already starts with the PathBase is kept as is, so URLs that were prefixed manually keep working. + /// + protected virtual string GetExternalFileUrl(ViewContext viewContext, BundleFile file) + { + var fileName = file.FileName; + if (fileName.StartsWith("~/", StringComparison.Ordinal)) + { + return viewContext.GetUrlHelper().Content(fileName); + } + + if (fileName.StartsWith("/", StringComparison.Ordinal) && !fileName.StartsWith("//", StringComparison.Ordinal)) + { + var pathBase = viewContext.HttpContext.Request.PathBase; + var queryIndex = fileName.IndexOfAny(['?', '#']); + var path = new PathString(queryIndex < 0 ? fileName : fileName.Substring(0, queryIndex)); + if (pathBase.HasValue && path.StartsWithSegments(pathBase)) + { + return fileName; + } + + return viewContext.GetUrlHelper().Content("~" + fileName); + } + + return fileName; + } + protected virtual string GenerateBundleName(List bundleItems) { return bundleItems.JoinAsString("|").ToMd5(); diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperScriptService.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperScriptService.cs index 0e87d88370..f6ba47c613 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperScriptService.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperScriptService.cs @@ -54,7 +54,7 @@ public class AbpTagHelperScriptService : AbpTagHelperResourceService var nonceText = (viewContext.HttpContext.Items.TryGetValue(AbpAspNetCoreConsts.ScriptNonceKey, out var nonce) && nonce is string nonceString && !string.IsNullOrEmpty(nonceString)) ? $"nonce=\"{nonceString}\" " : string.Empty; - var src = file.IsExternalFile ? file.FileName : viewContext.GetUrlHelper().Content((file.FileName + "?_v=" + fileInfo!.LastModified.UtcTicks).EnsureStartsWith('~')); + var src = file.IsExternalFile ? GetExternalFileUrl(viewContext, file) : viewContext.GetUrlHelper().Content((file.FileName + "?_v=" + fileInfo!.LastModified.UtcTicks).EnsureStartsWith('~')); output.Content.AppendHtml($"{Environment.NewLine}"); } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperStyleService.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperStyleService.cs index a61d000d6b..b19aa97fe7 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperStyleService.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperStyleService.cs @@ -52,7 +52,7 @@ public class AbpTagHelperStyleService : AbpTagHelperResourceService _ => false }; - var href = file.IsExternalFile ? file.FileName : viewContext.GetUrlHelper().Content((file.FileName + "?_v=" + fileInfo!.LastModified.UtcTicks).EnsureStartsWith('~')); + var href = file.IsExternalFile ? GetExternalFileUrl(viewContext, file) : viewContext.GetUrlHelper().Content((file.FileName + "?_v=" + fileInfo!.LastModified.UtcTicks).EnsureStartsWith('~')); if (preload || Options.PreloadStylesByDefault || Options.PreloadStyles.Any(x => file.FileName.StartsWith(x, StringComparison.OrdinalIgnoreCase))) { output.Content.AppendHtml(SecurityHeadersOptions.UseContentSecurityPolicyScriptNonce 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"); + } +} diff --git a/framework/test/Volo.Abp.AspNetCore.Components.Web.Theming.Tests/Volo/Abp/AspNetCore/Components/Web/MenuItemUrlHelper_Tests.cs b/framework/test/Volo.Abp.AspNetCore.Components.Web.Theming.Tests/Volo/Abp/AspNetCore/Components/Web/MenuItemUrlHelper_Tests.cs new file mode 100644 index 0000000000..0874819b7d --- /dev/null +++ b/framework/test/Volo.Abp.AspNetCore.Components.Web.Theming.Tests/Volo/Abp/AspNetCore/Components/Web/MenuItemUrlHelper_Tests.cs @@ -0,0 +1,27 @@ +using Shouldly; +using Xunit; + +namespace Volo.Abp.AspNetCore.Components.Web; + +public class MenuItemUrlHelper_Tests +{ + [Theory] + [InlineData("/", "")] + [InlineData("/books", "books")] + [InlineData("~/books", "books")] + [InlineData("books", "books")] + [InlineData("/identity/users?filter=a", "identity/users?filter=a")] + [InlineData("//cdn.example.com/help", "//cdn.example.com/help")] + [InlineData("https://docs.example.com/help", "https://docs.example.com/help")] + [InlineData("mailto:info@example.com", "mailto:info@example.com")] + public void Should_Make_Local_Urls_Relative_To_Base(string url, string expected) + { + MenuItemUrlHelper.ToBaseRelativeUrl(url).ShouldBe(expected); + } + + [Fact] + public void Should_Return_Null_For_Null_Url() + { + MenuItemUrlHelper.ToBaseRelativeUrl(null).ShouldBeNull(); + } +} diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.UI.Tests/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperResourceService_ExternalFile_Tests.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.UI.Tests/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperResourceService_ExternalFile_Tests.cs new file mode 100644 index 0000000000..de8ef9aa0c --- /dev/null +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.UI.Tests/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperResourceService_ExternalFile_Tests.cs @@ -0,0 +1,125 @@ +#nullable enable + +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc.Abstractions; +using Microsoft.AspNetCore.Mvc.Rendering; +using Microsoft.AspNetCore.Mvc.Routing; +using Microsoft.AspNetCore.Razor.TagHelpers; +using Microsoft.AspNetCore.Routing; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using Shouldly; +using Volo.Abp.AspNetCore.Security; +using Xunit; + +namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers; + +public class AbpTagHelperResourceService_ExternalFile_Tests +{ + [Theory] + [InlineData("/themes/basic/googlefonts.css", "", "/themes/basic/googlefonts.css")] + [InlineData("/themes/basic/googlefonts.css", "/foo", "/foo/themes/basic/googlefonts.css")] + [InlineData("~/themes/basic/googlefonts.css", "/foo", "/foo/themes/basic/googlefonts.css")] + [InlineData("/themes/basic/googlefonts.css", "/foo/bar", "/foo/bar/themes/basic/googlefonts.css")] + [InlineData("https://fonts.googleapis.com/css2?family=Lexend", "/foo", "https://fonts.googleapis.com/css2?family=Lexend")] + [InlineData("//cdn.example.com/foo.css", "/foo", "//cdn.example.com/foo.css")] + [InlineData("main.css", "/foo", "main.css")] + [InlineData("/foo/themes/basic/googlefonts.css", "/foo", "/foo/themes/basic/googlefonts.css")] + [InlineData("/foo/bar/themes/basic/googlefonts.css", "/foo/bar", "/foo/bar/themes/basic/googlefonts.css")] + [InlineData("/foo/themes/basic/googlefonts.css?v=1", "/foo", "/foo/themes/basic/googlefonts.css?v=1")] + [InlineData("/foobar/themes/basic/googlefonts.css", "/foo", "/foo/foobar/themes/basic/googlefonts.css")] + [InlineData("~/themes/basic/googlefonts.css", "", "/themes/basic/googlefonts.css")] + [InlineData("~/themes/basic/googlefonts.css", "/themes", "/themes/themes/basic/googlefonts.css")] + [InlineData("~/themes/basic/googlefonts.css", "/themes/basic", "/themes/basic/themes/basic/googlefonts.css")] + public void Style_Should_Resolve_Local_External_File_With_PathBase(string fileName, string pathBase, string expected) + { + var service = new TestStyleService(); + + var output = CreateOutput(); + service.AddTag(CreateViewContext(pathBase), output, new BundleFile(fileName, true)); + + output.Content.GetContent().ShouldContain($"href=\"{expected}\""); + } + + [Theory] + [InlineData("/libs/external.js", "", "/libs/external.js")] + [InlineData("/libs/external.js", "/foo", "/foo/libs/external.js")] + [InlineData("~/libs/external.js", "/foo", "/foo/libs/external.js")] + [InlineData("https://cdn.example.com/external.js", "/foo", "https://cdn.example.com/external.js")] + [InlineData("//cdn.example.com/external.js", "/foo", "//cdn.example.com/external.js")] + [InlineData("/foo/libs/external.js", "/foo", "/foo/libs/external.js")] + [InlineData("/foo/bar/libs/external.js", "/foo/bar", "/foo/bar/libs/external.js")] + [InlineData("/foobar/libs/external.js", "/foo", "/foo/foobar/libs/external.js")] + public void Script_Should_Resolve_Local_External_File_With_PathBase(string fileName, string pathBase, string expected) + { + var service = new TestScriptService(); + + var output = CreateOutput(); + service.AddTag(CreateViewContext(pathBase), output, new BundleFile(fileName, true)); + + output.Content.GetContent().ShouldContain($"src=\"{expected}\""); + } + + private static ViewContext CreateViewContext(string pathBase) + { + var httpContext = new DefaultHttpContext + { + RequestServices = new ServiceCollection() + .AddSingleton() + .BuildServiceProvider() + }; + httpContext.Request.PathBase = new PathString(pathBase); + + return new ViewContext + { + HttpContext = httpContext, + RouteData = new RouteData(), + ActionDescriptor = new ActionDescriptor() + }; + } + + private static TagHelperOutput CreateOutput() + { + return new TagHelperOutput( + "abp-test", + new TagHelperAttributeList(), + (_, _) => Task.FromResult(new DefaultTagHelperContent())); + } + + private static TagHelperContext CreateContext() + { + return new TagHelperContext(new TagHelperAttributeList(), new Dictionary(), "test"); + } + + private class TestTagHelper : TagHelper + { + } + + private class TestStyleService : AbpTagHelperStyleService + { + public TestStyleService() + : base(null!, Microsoft.Extensions.Options.Options.Create(new AbpBundlingOptions()), null!, Microsoft.Extensions.Options.Options.Create(new AbpSecurityHeadersOptions())) + { + } + + public void AddTag(ViewContext viewContext, TagHelperOutput output, BundleFile file) + { + AddHtmlTag(viewContext, new TestTagHelper(), CreateContext(), output, file, null); + } + } + + private class TestScriptService : AbpTagHelperScriptService + { + public TestScriptService() + : base(null!, Microsoft.Extensions.Options.Options.Create(new AbpBundlingOptions()), null!) + { + } + + public void AddTag(ViewContext viewContext, TagHelperOutput output, BundleFile file) + { + AddHtmlTag(viewContext, new TestTagHelper(), CreateContext(), output, file, null); + } + } +} diff --git a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Server.BasicTheme/Themes/Basic/LoginDisplay.razor b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Server.BasicTheme/Themes/Basic/LoginDisplay.razor index f3b2c41779..378146de2f 100644 --- a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Server.BasicTheme/Themes/Basic/LoginDisplay.razor +++ b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Server.BasicTheme/Themes/Basic/LoginDisplay.razor @@ -1,4 +1,5 @@ @namespace Volo.Abp.AspNetCore.Components.Server.BasicTheme.Themes.Basic +@using Volo.Abp.AspNetCore.Components.Web @using Volo.Abp.Users @using Volo.Abp.MultiTenancy @using Volo.Abp.UI.Navigation @@ -34,7 +35,7 @@ } else { - @menuItem.DisplayName + @menuItem.DisplayName } } } diff --git a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Server.MudBlazorBasicTheme/Themes/Basic/LoginDisplay.razor b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Server.MudBlazorBasicTheme/Themes/Basic/LoginDisplay.razor index ee05fab1c8..3f24cb195b 100644 --- a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Server.MudBlazorBasicTheme/Themes/Basic/LoginDisplay.razor +++ b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Server.MudBlazorBasicTheme/Themes/Basic/LoginDisplay.razor @@ -1,4 +1,5 @@ @namespace Volo.Abp.AspNetCore.Components.Server.MudBlazorBasicTheme.Themes.Basic +@using Volo.Abp.AspNetCore.Components.Web @using Volo.Abp.Users @using Volo.Abp.MultiTenancy @using Microsoft.Extensions.Localization @@ -39,7 +40,7 @@ } else { - @menuItem.DisplayName + @menuItem.DisplayName } } } diff --git a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Web.BasicTheme/Themes/Basic/FirstLevelNavMenuItem.razor b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Web.BasicTheme/Themes/Basic/FirstLevelNavMenuItem.razor index 32de8507cb..9bb8f0f0c6 100644 --- a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Web.BasicTheme/Themes/Basic/FirstLevelNavMenuItem.razor +++ b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Web.BasicTheme/Themes/Basic/FirstLevelNavMenuItem.razor @@ -17,7 +17,7 @@ else @if (MenuItem.Url != null) { - var url = MenuItem.Url == null ? "#" : MenuItem.Url.TrimStart('/', '~'); + var url = MenuItemUrlHelper.ToBaseRelativeUrl(MenuItem.Url) ?? "#"; @if (MenuItem.Icon != null && MenuItem.Icon.StartsWith("fa")) { @@ -50,7 +50,7 @@ else } else { - var itemUrl = item.Url == null ? "#" : item.Url.TrimStart('/', '~'); + var itemUrl = MenuItemUrlHelper.ToBaseRelativeUrl(item.Url) ?? "#"; @if (item.Icon != null) { diff --git a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Web.BasicTheme/Themes/Basic/SecondLevelNavMenuItem.razor b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Web.BasicTheme/Themes/Basic/SecondLevelNavMenuItem.razor index 2e6f783556..0f70bbabba 100644 --- a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Web.BasicTheme/Themes/Basic/SecondLevelNavMenuItem.razor +++ b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Web.BasicTheme/Themes/Basic/SecondLevelNavMenuItem.razor @@ -31,7 +31,7 @@ else } else { - var itemUrl = item.Url == null ? "#" : item.Url.TrimStart('/', '~'); + var itemUrl = MenuItemUrlHelper.ToBaseRelativeUrl(item.Url) ?? "#"; @if (item.Icon != null) { diff --git a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Web.MudBlazorBasicTheme/Themes/Basic/NavMenu.razor b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Web.MudBlazorBasicTheme/Themes/Basic/NavMenu.razor index 1ffc0db3a3..f2ceb1eef8 100644 --- a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Web.MudBlazorBasicTheme/Themes/Basic/NavMenu.razor +++ b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Web.MudBlazorBasicTheme/Themes/Basic/NavMenu.razor @@ -5,7 +5,7 @@ { if (menuItem.Items.IsNullOrEmpty()) { - + @menuItem.DisplayName diff --git a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Web.MudBlazorBasicTheme/Themes/Basic/NavMenuItem.razor b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Web.MudBlazorBasicTheme/Themes/Basic/NavMenuItem.razor index 20d854dcd4..6df5378165 100644 --- a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Web.MudBlazorBasicTheme/Themes/Basic/NavMenuItem.razor +++ b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Web.MudBlazorBasicTheme/Themes/Basic/NavMenuItem.razor @@ -28,7 +28,7 @@ else } else { - + @item.DisplayName diff --git a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/LoginDisplay.razor.cs b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/LoginDisplay.razor.cs index 7fe86a6ecc..3c710e310b 100644 --- a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/LoginDisplay.razor.cs +++ b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/LoginDisplay.razor.cs @@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Routing; using Microsoft.AspNetCore.Components.WebAssembly.Authentication; using Microsoft.JSInterop; +using Volo.Abp.AspNetCore.Components.Web; using Volo.Abp.AspNetCore.Components.Web.Security; using Volo.Abp.AspNetCore.Components.WebAssembly; using Volo.Abp.UI.Navigation; @@ -60,7 +61,7 @@ public partial class LoginDisplay : IDisposable private async Task NavigateToAsync(string uri, string target = null) { - uri = uri?.TrimStart('~', '/') ?? uri; + uri = MenuItemUrlHelper.ToBaseRelativeUrl(uri); if (target == "_blank") { diff --git a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.MudBlazorBasicTheme/Themes/Basic/LoginDisplay.razor.cs b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.MudBlazorBasicTheme/Themes/Basic/LoginDisplay.razor.cs index 32c60bd407..ec997023e3 100644 --- a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.MudBlazorBasicTheme/Themes/Basic/LoginDisplay.razor.cs +++ b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.MudBlazorBasicTheme/Themes/Basic/LoginDisplay.razor.cs @@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Routing; using Microsoft.AspNetCore.Components.WebAssembly.Authentication; using Microsoft.JSInterop; +using Volo.Abp.AspNetCore.Components.Web; using Volo.Abp.AspNetCore.Components.Web.Security; using Volo.Abp.UI.Navigation; @@ -47,9 +48,11 @@ public partial class LoginDisplay : IDisposable private async Task NavigateToAsync(string uri, string target = null) { + uri = MenuItemUrlHelper.ToBaseRelativeUrl(uri); + if (target == "_blank") { - await JsRuntime.InvokeVoidAsync("open", uri, target); + await JsRuntime.InvokeVoidAsync("open", Navigation.ToAbsoluteUri(uri).ToString(), target); } else {