using System.Reflection; using System.Text; using Abp.Extensions; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.SwaggerUI; using VberZero.Configuration; namespace VberAdmin.Swagger { public static class SwaggerExtensions { private static string _apiVersion = "v1"; /// /// 将 ABP 基础 URI 注入 index.html 页面 /// /// /// base path (URL) to application API public static void InjectBaseUrl(this SwaggerUIOptions options, string pathBase) { pathBase = pathBase.EnsureEndsWith('/'); options.HeadContent = new StringBuilder(options.HeadContent) .AppendLine($"") .ToString(); } /// /// 在 Configure 方法中启用此行和相关行以启用 swagger UI /// /// /// public static void ConfigureSwagger(this IServiceCollection services, IWebHostEnvironment env) { var appConfiguration = env.GetAppConfiguration(); services.AddSwaggerGen(options => { options.SwaggerDoc(_apiVersion, new OpenApiInfo { Version = _apiVersion, Title = "VberAdmin API", Description = "VberAdmin", // uncomment if needed TermsOfService = new Uri("https://example.com/terms"), //Contact = new OpenApiContact //{ // Name = "VberAdmin", // Email = string.Empty, // Url = new Uri("https://twitter.com/aspboilerplate"), //}, //License = new OpenApiLicense //{ // Name = "MIT License", // Url = new Uri("https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/LICENSE"), //} }); options.DocInclusionPredicate((docName, description) => true); // 定义正在使用的 BearerAuth 方案 options.AddSecurityDefinition("bearerAuth", new OpenApiSecurityScheme() { Description = "使用 Bearer 方案的 JWT 授权标头。 例子: \"Authorization: Bearer {token}\"", Name = "Authorization", In = ParameterLocation.Header, Type = SecuritySchemeType.ApiKey }); options.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "bearerAuth" } }, new string[] { } } }); //添加摘要 bool canShowSummaries = appConfiguration.GetValue("Swagger:ShowSummaries"); if (canShowSummaries) { var hostXmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; var hostXmlPath = Path.Combine(env.ContentRootPath, hostXmlFile); options.IncludeXmlComments(hostXmlPath); var applicationXml = $"VberAdmin.Application.xml"; var applicationXmlPath = Path.Combine(env.ContentRootPath, applicationXml); options.IncludeXmlComments(applicationXmlPath); var webCoreXmlFile = $"VberAdmin.Web.Core.xml"; var webCoreXmlPath = Path.Combine(env.ContentRootPath, webCoreXmlFile); options.IncludeXmlComments(webCoreXmlPath); } options.ParameterFilter(); options.SchemaFilter(); options.OperationFilter(); options.OperationFilter(); options.DocumentFilter(); }); } public static void Use_SwaggerUI(this IApplicationBuilder app, Assembly assembly, string namespaceStr = "VberAdmin.Web.Mvc") { //启用中间件以将生成的 Swagger 作为 JSON 端点提供服务 app.UseSwagger(c => { c.RouteTemplate = "doc/{documentName}/api.json"; }); // 启用中间件以提供 swagger-ui 资产(HTML、JS、CSS 等) app.UseSwaggerUI(options => { options.DocumentTitle = "API-系统管理平台"; options.DocExpansion(DocExpansion.None);//设置为none可折叠所有方法 options.DefaultModelsExpandDepth(-1); //设置为 -1 可不显示models options.RoutePrefix = "doc";//配置后,你的最终访问路径就就是 /doc/index.html 否则 /swagger; options.SwaggerEndpoint($"/doc/{_apiVersion}/api.json", $"VberAdmin API {_apiVersion}"); options.IndexStream = () => assembly.GetManifestResourceStream($"{namespaceStr}.wwwroot.swagger.ui.index.html"); options.DisplayRequestDuration(); // Controls the display of the request duration (in milliseconds) for "Try it out" requests. }); // URL: /doc } } }