diff --git a/Infrastructure/GlobalConstant.cs b/Infrastructure/GlobalConstant.cs
index 900c53f..bfb8039 100644
--- a/Infrastructure/GlobalConstant.cs
+++ b/Infrastructure/GlobalConstant.cs
@@ -17,5 +17,9 @@ namespace Infrastructure
/// 管理员角色
///
public static string AdminRole = "admin";
+ ///
+ /// 开发版本API映射路径
+ ///
+ public static string DevApiProxy = "/dev-api/";
}
}
diff --git a/ZR.Admin.WebApi/Extensions/SwaggerExtension.cs b/ZR.Admin.WebApi/Extensions/SwaggerExtension.cs
index c49c5ed..13598a4 100644
--- a/ZR.Admin.WebApi/Extensions/SwaggerExtension.cs
+++ b/ZR.Admin.WebApi/Extensions/SwaggerExtension.cs
@@ -1,5 +1,6 @@
using Infrastructure;
using Microsoft.AspNetCore.Authentication.JwtBearer;
+using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
@@ -7,13 +8,40 @@ using Swashbuckle.AspNetCore.Filters;
using System;
using System.Collections.Generic;
using System.IO;
-using System.Linq;
-using System.Threading.Tasks;
namespace ZR.Admin.WebApi.Extensions
{
public static class SwaggerExtension
{
+ ///
+ ///
+ ///
+ ///
+ public static void UseSwagger(this IApplicationBuilder app)
+ {
+ app.UseSwagger(c =>
+ {
+ c.RouteTemplate = "swagger/{documentName}/swagger.json";
+ c.PreSerializeFilters.Add((swaggerDoc, httpReq) =>
+ {
+ var url = $"{httpReq.Scheme}://{httpReq.Host.Value}";
+ var referer = httpReq.Headers["Referer"].ToString();
+ if (referer.Contains(GlobalConstant.DevApiProxy))
+ url = referer.Substring(0,
+ referer.IndexOf(GlobalConstant.DevApiProxy, StringComparison.InvariantCulture) + GlobalConstant.DevApiProxy.Length - 1);
+ swaggerDoc.Servers =
+ new List
+ {
+ new OpenApiServer
+ {
+ Url = url
+ }
+ };
+ });
+ });
+ app.UseSwaggerUI(c => c.SwaggerEndpoint("v1/swagger.json", "ZrAdmin v1"));
+ }
+
public static void AddSwaggerConfig(this IServiceCollection services)
{
if (services == null) throw new ArgumentNullException(nameof(services));
diff --git a/ZR.Admin.WebApi/Startup.cs b/ZR.Admin.WebApi/Startup.cs
index e7bab74..6db363f 100644
--- a/ZR.Admin.WebApi/Startup.cs
+++ b/ZR.Admin.WebApi/Startup.cs
@@ -14,6 +14,7 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
+using Microsoft.OpenApi.Models;
using ZR.Admin.WebApi.Extensions;
using ZR.Admin.WebApi.Filters;
using ZR.Admin.WebApi.Framework;
@@ -93,9 +94,8 @@ namespace ZR.Admin.WebApi
{
app.UseDeveloperExceptionPage();
}
- app.UseSwagger();
- app.UseSwaggerUI(c => c.SwaggerEndpoint("v1/swagger.json", "ZrAdmin v1"));
+ app.UseSwagger();
//ʹԶζȥbody
app.Use((context, next) =>
{