|
|
|
@ -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 |
|
|
|
/// </summary>
|
|
|
|
/// <param name="options">The Swagger UI options.</param>
|
|
|
|
/// <param name="appPath">The application base path.</param>
|
|
|
|
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("<script>"); |
|
|
|
builder.AppendLine(" var abp = abp || {};"); |
|
|
|
builder.AppendLine($" abp.appPath = {JsonSerializer.Serialize(normalizedAppPath)};"); |
|
|
|
builder.AppendLine("</script>"); |
|
|
|
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("<script>"); |
|
|
|
builder.AppendLine(" var abp = abp || {};"); |
|
|
|
builder.AppendLine($" abp.appPath = {JsonSerializer.Serialize(normalizedAppPath)};"); |
|
|
|
builder.AppendLine("</script>"); |
|
|
|
return builder.ToString(); |
|
|
|
} |
|
|
|
} |
|
|
|
|