diff --git a/src/blazor/admin/BootstrapAdmin.Web/BootstrapAdmin.Web.csproj b/src/blazor/admin/BootstrapAdmin.Web/BootstrapAdmin.Web.csproj index 144d8eb0..448f9f25 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/BootstrapAdmin.Web.csproj +++ b/src/blazor/admin/BootstrapAdmin.Web/BootstrapAdmin.Web.csproj @@ -8,6 +8,7 @@ + diff --git a/src/blazor/admin/BootstrapAdmin.Web/Extensions/ApplicationBuilderExtensions.cs b/src/blazor/admin/BootstrapAdmin.Web/Extensions/ApplicationBuilderExtensions.cs index 21e45ab8..d4742545 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Extensions/ApplicationBuilderExtensions.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Extensions/ApplicationBuilderExtensions.cs @@ -1,4 +1,6 @@ -namespace Microsoft.AspNetCore.Builder; +using BootstrapAdmin.Web; + +namespace Microsoft.AspNetCore.Builder; /// /// @@ -20,6 +22,8 @@ public static class ApplicationBuilderExtensions builder.UseAuthentication(); builder.UseAuthorization(); + builder.UseSwagger(builder.Configuration["SwaggerPathBase"].TrimEnd('/')); + return builder; } } diff --git a/src/blazor/admin/BootstrapAdmin.Web/Extensions/ServiceCollectionExtensions.cs b/src/blazor/admin/BootstrapAdmin.Web/Extensions/ServiceCollectionExtensions.cs index 8bc19d29..95f372d9 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Extensions/ServiceCollectionExtensions.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Extensions/ServiceCollectionExtensions.cs @@ -1,4 +1,5 @@ -using BootstrapAdmin.Web.Core.Services; +using BootstrapAdmin.Web; +using BootstrapAdmin.Web.Core.Services; using BootstrapAdmin.Web.HealthChecks; using BootstrapAdmin.Web.Services; using BootstrapAdmin.Web.Services.SMS; @@ -23,6 +24,7 @@ namespace Microsoft.Extensions.DependencyInjection services.AddLogging(logging => logging.AddFileLogger().AddCloudLogger().AddDBLogger(ExceptionsHelper.Log)); services.AddCors(); services.AddResponseCompression(); + services.AddControllers(); // 增加后台任务 services.AddTaskServices(); @@ -40,6 +42,8 @@ namespace Microsoft.Extensions.DependencyInjection // 增加认证授权服务 services.AddBootstrapAdminSecurity(); + services.AddSwagger(); + // 增加 BootstrapApp 上下文服务 services.AddScoped(); diff --git a/src/blazor/admin/BootstrapAdmin.Web/Extensions/SwaggerExtensions.cs b/src/blazor/admin/BootstrapAdmin.Web/Extensions/SwaggerExtensions.cs new file mode 100644 index 00000000..13d5fc1f --- /dev/null +++ b/src/blazor/admin/BootstrapAdmin.Web/Extensions/SwaggerExtensions.cs @@ -0,0 +1,72 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.OpenApi.Models; +using System; +using System.IO; + +namespace BootstrapAdmin.Web +{ + /// + /// Swagger 扩展方法 + /// + internal static class SwaggerExtensions + { + /// + /// Swagger 中间件 + /// + /// + /// + public static void UseSwagger(this IApplicationBuilder app, string pathBase) + { + app.UseSwagger(); + app.UseSwaggerUI(c => + { + c.SwaggerEndpoint($"{pathBase}/swagger/v1/swagger.json", "BootstrapAdmin API V1"); + }); + } + + /// + /// 注入 Swagger 服务到容器内 + /// + /// + public static void AddSwagger(this IServiceCollection services) + { + services.AddSwaggerGen(options => + { + options.SwaggerDoc("v1", new OpenApiInfo + { + Version = "v1", + Title = "BootstrapAdmin API" + }); + + //Set the comments path for the swagger json and ui. + var xmlPath = Path.Combine(AppContext.BaseDirectory, "BootstrapAdmin.xml"); + options.IncludeXmlComments(xmlPath); + + options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme + { + Name = "Authorization", + Type = SecuritySchemeType.ApiKey, + Scheme = "Bearer", + BearerFormat = "JWT", + In = ParameterLocation.Header, + Description = "JWT Authorization header using the Bearer scheme." + }); + options.AddSecurityRequirement(new OpenApiSecurityRequirement + { + { + new OpenApiSecurityScheme + { + Reference = new OpenApiReference + { + Type = ReferenceType.SecurityScheme, + Id = "Bearer" + } + }, + new string[]{ } + } + }); + }); + } + } +}