From bdac30bc14d907fbebb10e98d4921fcfb0e2e97d Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 4 Feb 2026 11:47:32 +0800 Subject: [PATCH 1/2] Add AbpAppPath extension method and update appPath handling in Swagger UI scripts --- .../AbpSwaggerUIOptionsExtensions.cs | 30 +++++++++++++++++++ .../wwwroot/swagger/ui/abp.js | 6 +--- .../wwwroot/swagger/ui/abp.swagger.js | 2 +- 3 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerUIOptionsExtensions.cs diff --git a/framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerUIOptionsExtensions.cs b/framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerUIOptionsExtensions.cs new file mode 100644 index 0000000000..b73cf8a7d0 --- /dev/null +++ b/framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerUIOptionsExtensions.cs @@ -0,0 +1,30 @@ +using System; +using System.Text; +using System.Text.Json; +using Swashbuckle.AspNetCore.SwaggerUI; + +namespace Microsoft.Extensions.DependencyInjection; + +public static class AbpSwaggerUIOptionsExtensions +{ + /// + /// Sets the abp.appPath used by the Swagger UI scripts. + /// + /// The Swagger UI options. + /// The application base path. + public static void AbpAppPath(this SwaggerUIOptions options, string appPath) + { + var normalizedAppPath = NormalizeAppPath(appPath); + var builder = new StringBuilder(options.HeadContent ?? string.Empty); + builder.AppendLine(""); + options.HeadContent = builder.ToString(); + } + + private static string NormalizeAppPath(string appPath) + { + return string.IsNullOrWhiteSpace(appPath) ? "/" : appPath.Trim().EnsureStartsWith('/').EnsureEndsWith('/'); + } +} diff --git a/framework/src/Volo.Abp.Swashbuckle/wwwroot/swagger/ui/abp.js b/framework/src/Volo.Abp.Swashbuckle/wwwroot/swagger/ui/abp.js index 11cbe56803..da996ea6f5 100644 --- a/framework/src/Volo.Abp.Swashbuckle/wwwroot/swagger/ui/abp.js +++ b/framework/src/Volo.Abp.Swashbuckle/wwwroot/swagger/ui/abp.js @@ -2,11 +2,7 @@ var abp = abp || {}; (function () { /* Application paths *****************************************/ - - //Current application root path (including virtual directory if exists). - var baseElement = document.querySelector('base'); - var baseHref = baseElement ? baseElement.getAttribute('href') : null; - abp.appPath = baseHref || abp.appPath || '/'; + abp.appPath = abp.appPath || '/'; /* UTILS ***************************************************/ diff --git a/framework/src/Volo.Abp.Swashbuckle/wwwroot/swagger/ui/abp.swagger.js b/framework/src/Volo.Abp.Swashbuckle/wwwroot/swagger/ui/abp.swagger.js index db054056d1..e961f6bc2d 100644 --- a/framework/src/Volo.Abp.Swashbuckle/wwwroot/swagger/ui/abp.swagger.js +++ b/framework/src/Volo.Abp.Swashbuckle/wwwroot/swagger/ui/abp.swagger.js @@ -11,7 +11,7 @@ var abp = abp || {}; var oidcSupportedScopes = configObject.oidcSupportedScopes || []; var oidcDiscoveryEndpoint = configObject.oidcDiscoveryEndpoint || []; var tenantPlaceHolders = ["{{tenantId}}", "{{tenantName}}", "{0}"] - abp.appPath = configObject.baseUrl || abp.appPath; + abp.appPath = abp.appPath || "/"; var requestInterceptor = configObject.requestInterceptor; var responseInterceptor = configObject.responseInterceptor; From 288cef294014289fda7af06298d1a78e8aabdcb4 Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 4 Feb 2026 12:19:12 +0800 Subject: [PATCH 2/2] Refactor AbpAppPath method to include null checks and streamline script generation --- .../AbpSwaggerUIOptionsExtensions.cs | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerUIOptionsExtensions.cs b/framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerUIOptionsExtensions.cs index b73cf8a7d0..d307bc7e34 100644 --- a/framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerUIOptionsExtensions.cs +++ b/framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerUIOptionsExtensions.cs @@ -1,7 +1,9 @@ using System; using System.Text; using System.Text.Json; +using JetBrains.Annotations; using Swashbuckle.AspNetCore.SwaggerUI; +using Volo.Abp; namespace Microsoft.Extensions.DependencyInjection; @@ -12,19 +14,34 @@ public static class AbpSwaggerUIOptionsExtensions /// /// The Swagger UI options. /// The application base path. - public static void AbpAppPath(this SwaggerUIOptions options, string appPath) + public static void AbpAppPath([NotNull] this SwaggerUIOptions options, [NotNull] string appPath) { + Check.NotNull(options, nameof(options)); + Check.NotNull(appPath, nameof(appPath)); + var normalizedAppPath = NormalizeAppPath(appPath); - var builder = new StringBuilder(options.HeadContent ?? string.Empty); - builder.AppendLine(""); - options.HeadContent = builder.ToString(); + options.HeadContent = BuildAppPathScript(normalizedAppPath, options.HeadContent ?? string.Empty); } private static string NormalizeAppPath(string appPath) { - return string.IsNullOrWhiteSpace(appPath) ? "/" : appPath.Trim().EnsureStartsWith('/').EnsureEndsWith('/'); + return string.IsNullOrWhiteSpace(appPath) + ? "/" + : appPath.Trim().EnsureStartsWith('/').EnsureEndsWith('/'); + } + + private static string BuildAppPathScript(string normalizedAppPath, string headContent) + { + var builder = new StringBuilder(headContent); + if (builder.Length > 0) + { + builder.AppendLine(); + } + + builder.AppendLine(""); + return builder.ToString(); } }