Commit 2c602f14 by wyc

SwaggerOpen

parent a7e048c2
...@@ -10,32 +10,34 @@ namespace Performance.Api.Configurations ...@@ -10,32 +10,34 @@ namespace Performance.Api.Configurations
{ {
public static class SwaggerConfig public static class SwaggerConfig
{ {
public static void AddSwaggerConfiguration(this IServiceCollection services) public static void AddSwaggerConfiguration(this IServiceCollection services, IConfiguration configuration)
{ {
if (services == null) throw new ArgumentNullException(nameof(services)); if (configuration.GetValue("SwaggerOpen", true))
services.AddSwaggerGen(c =>
{ {
c.SwaggerDoc("v1", new OpenApiInfo { Version = "v1.0", Title = "绩效API接口" }); if (services == null) throw new ArgumentNullException(nameof(services));
var xmlPath = new string[] services.AddSwaggerGen(c =>
{ {
c.SwaggerDoc("v1", new OpenApiInfo { Version = "v1.0", Title = "绩效API接口" });
var xmlPath = new string[]
{
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot", "Performance.Api.xml"), Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot", "Performance.Api.xml"),
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot", "Performance.DtoModels.xml"), Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot", "Performance.DtoModels.xml"),
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot", "Performance.EntityModels.xml"), Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot", "Performance.EntityModels.xml"),
}; };
foreach (var item in xmlPath) foreach (var item in xmlPath)
{ {
c.IncludeXmlComments(item, true); c.IncludeXmlComments(item, true);
} }
//var xmlPathsss = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot", "Performance.Api.xml"); //var xmlPathsss = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot", "Performance.Api.xml");
//c.IncludeXmlComments(xmlPathsss, true); //c.IncludeXmlComments(xmlPathsss, true);
// Token绑定到ConfigureServices // Token绑定到ConfigureServices
var security = new OpenApiSecurityRequirement var security = new OpenApiSecurityRequirement
{ {
{ {
new OpenApiSecurityScheme new OpenApiSecurityScheme
{ {
...@@ -43,28 +45,30 @@ public static void AddSwaggerConfiguration(this IServiceCollection services) ...@@ -43,28 +45,30 @@ public static void AddSwaggerConfiguration(this IServiceCollection services)
}, },
new List<string>() new List<string>()
} }
}; };
c.AddSecurityRequirement(security); c.AddSecurityRequirement(security);
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{ {
Description = "JWT授权(数据将在请求头中进行传输) 直接在下框中输入Bearer {token}(注意两者之间是一个空格)", Description = "JWT授权(数据将在请求头中进行传输) 直接在下框中输入Bearer {token}(注意两者之间是一个空格)",
Name = "Authorization", Name = "Authorization",
In = ParameterLocation.Header In = ParameterLocation.Header
});
}); });
}); }
} }
public static void UseSwaggerSetup(this IApplicationBuilder app, IConfiguration configuration) public static void UseSwaggerSetup(this IApplicationBuilder app, IConfiguration configuration)
{ {
if (app == null) throw new ArgumentNullException(nameof(app)); if (app == null) throw new ArgumentNullException(nameof(app));
if (configuration.GetValue("SwaggerOpen", true))
app.UseSwagger();
app.UseSwaggerUI(c =>
{ {
c.SwaggerEndpoint(configuration["Application:SwaggerEndpoint"], "v1.0"); app.UseSwagger();
c.RoutePrefix = string.Empty; app.UseSwaggerUI(c =>
}); {
c.SwaggerEndpoint(configuration["Application:SwaggerEndpoint"], "v1.0");
c.RoutePrefix = string.Empty;
});
}
} }
} }
} }
...@@ -81,7 +81,7 @@ public void ConfigureServices(IServiceCollection services) ...@@ -81,7 +81,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddAutoMapperConfiguration(); services.AddAutoMapperConfiguration();
// swagger // swagger
services.AddSwaggerConfiguration(); services.AddSwaggerConfiguration(Configuration);
// service repository // service repository
services.AddDependencyInjectionConfiguration(); services.AddDependencyInjectionConfiguration();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment