Browse Source

Refactor AbpAppPath method to include null checks and streamline script generation

pull/24799/head
maliming 3 days ago
parent
commit
288cef2940
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 33
      framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerUIOptionsExtensions.cs

33
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
/// </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();
}
}

Loading…
Cancel
Save