From ff47dac2bf4d0348ca3ef8c7cc3c8fb3790cb7d5 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Thu, 20 Jun 2019 08:43:18 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84swagger=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E4=BB=A3=E7=A0=81=E5=88=B0=E7=8B=AC=E7=AB=8B=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bootstrap.Admin/Startup.cs | 33 ++-------------- Bootstrap.Admin/SwaggerExtensions.cs | 58 ++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 30 deletions(-) create mode 100644 Bootstrap.Admin/SwaggerExtensions.cs diff --git a/Bootstrap.Admin/Startup.cs b/Bootstrap.Admin/Startup.cs index 55cfb62c..ff9b94b8 100644 --- a/Bootstrap.Admin/Startup.cs +++ b/Bootstrap.Admin/Startup.cs @@ -1,7 +1,6 @@ using Bootstrap.DataAccess; using Longbow.Web; using Longbow.Web.SignalR; -using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; @@ -12,9 +11,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; -using Swashbuckle.AspNetCore.Swagger; using System; -using System.IO; using System.Text.Encodings.Web; using System.Text.Unicode; @@ -64,6 +61,8 @@ namespace Bootstrap.Admin services.AddSignalRExceptionFilterHandler(async (client, ex) => await SignalRManager.Send(client, ex)); services.AddResponseCompression(); services.AddBootstrapAdminAuthentication(); + services.AddSwagger(); + services.AddButtonAuthorization(MenuHelper.AuthorizateButtons); services.AddMvc(options => { options.Filters.Add(); @@ -82,20 +81,6 @@ namespace Bootstrap.Admin option.AssumeDefaultVersionWhenUnspecified = true; option.ApiVersionReader = ApiVersionReader.Combine(new HeaderApiVersionReader("api-version"), new QueryStringApiVersionReader("api-version")); }); - services.AddSwaggerGen(options => - { - options.SwaggerDoc("v1", new Info - { - Version = "v1", - Title = "BootstrapAdmin API" - }); - - //Set the comments path for the swagger json and ui. - var xmlPath = Path.Combine(AppContext.BaseDirectory, "Bootstrap.Admin.xml"); - options.IncludeXmlComments(xmlPath); - options.OperationFilter(); // 添加httpHeader参数 - }); - services.AddButtonAuthorization(MenuHelper.AuthorizateButtons); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -125,25 +110,13 @@ namespace Bootstrap.Admin app.UseOnlineUsers(callback: TraceHelper.Save); app.UseCacheManagerCorsHandler(); app.UseSignalR(routes => { routes.MapHub("/NotiHub"); }); + app.UseSwagger(Configuration["SwaggerPathBase"].TrimEnd('/')); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); - app.UseWhen(context => context.Request.Path.StartsWithSegments("/swagger"), builder => - { - builder.Use(async (context, next) => - { - if (!context.User.Identity.IsAuthenticated) await context.ChallengeAsync(); - else await next(); - }); - }); - app.UseSwagger(); - app.UseSwaggerUI(c => - { - c.SwaggerEndpoint($"{Configuration["SwaggerPathBase"]}/swagger/v1/swagger.json", "BootstrapAdmin API V1"); - }); } } } diff --git a/Bootstrap.Admin/SwaggerExtensions.cs b/Bootstrap.Admin/SwaggerExtensions.cs new file mode 100644 index 00000000..bd770dd0 --- /dev/null +++ b/Bootstrap.Admin/SwaggerExtensions.cs @@ -0,0 +1,58 @@ +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; +using Swashbuckle.AspNetCore.Swagger; +using System; +using System.IO; + +namespace Bootstrap.Admin +{ + /// + /// + /// + internal static class SwaggerExtensions + { + /// + /// + /// + /// + /// + public static void UseSwagger(this IApplicationBuilder app, string pathBase) + { + app.UseWhen(context => context.Request.Path.StartsWithSegments("/swagger"), builder => + { + builder.Use(async (context, next) => + { + if (!context.User.Identity.IsAuthenticated) await context.ChallengeAsync(); + else await next(); + }); + }); + app.UseSwagger(); + app.UseSwaggerUI(c => + { + c.SwaggerEndpoint($"{pathBase}/swagger/v1/swagger.json", "BootstrapAdmin API V1"); + }); + } + + /// + /// + /// + /// + public static void AddSwagger(this IServiceCollection services) + { + services.AddSwaggerGen(options => + { + options.SwaggerDoc("v1", new Info + { + Version = "v1", + Title = "BootstrapAdmin API" + }); + + //Set the comments path for the swagger json and ui. + var xmlPath = Path.Combine(AppContext.BaseDirectory, "Bootstrap.Admin.xml"); + options.IncludeXmlComments(xmlPath); + options.OperationFilter(); // 添加httpHeader参数 + }); + } + } +}