You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.9 KiB
C#

using EntrustSettle.Common;
using Microsoft.AspNetCore.Builder;
using NLog;
using Swashbuckle.AspNetCore.SwaggerUI;
using System;
using System.IO;
namespace EntrustSettle.Extensions.Middlewares
{
/// <summary>
/// Swagger中间件
/// </summary>
public static class SwaggerMiddleware
{
public static void UseSwaggerMiddle(this IApplicationBuilder app, Func<Stream> streamHtml)
{
if (app == null) throw new ArgumentNullException(nameof(app));
app.UseSwagger();
app.UseSwaggerUI(c =>
{
SwaggerSetup.ApiInfos.ForEach(x =>
{
c.SwaggerEndpoint($"/swagger/{x.UrlPrefix}/swagger.json", x.Name);
});
// 将swagger首页设置成我们自定义的页面记得这个字符串的写法{项目名.swagger_index.html}
if (streamHtml.Invoke() == null)
{
var msg = "swagger_index.html的属性必须设置为嵌入的资源";
LogManager.GetCurrentClassLogger().Error(msg);
throw new Exception(msg);
}
c.IndexStream = streamHtml;
c.DocExpansion(DocExpansion.None); //->界面打开时自动折叠
if (Permissions.IsUseIds4)
{
c.OAuthClientId("blogadminjs");
}
//增加令牌本地缓存 reload不会丢失
c.ConfigObject.AdditionalItems.Add("persistAuthorization", "true");
// 路径配置设置为空表示直接在根域名localhost:8001访问该文件,注意localhost:8001/swagger是访问不到的去launchSettings.json把launchUrl去掉如果你想换一个路径直接写名字即可比如直接写c.RoutePrefix = "doc";
c.RoutePrefix = "";
});
}
}
}