(精华)2020年7月1日 ASP.NET Core Swagger的使用(Swashbuckle工具版)

首先安装包

Install-Package Swashbuckle.AspNetCore
public void ConfigureServices(IServiceCollection services)
{
   services.AddSwaggerGen(c =>
   {
       c.SwaggerDoc("v1", new OpenApiInfo
       {
           Version = "v1.0.0",
           Title = "接口文档"
       });
       // 这里是给Swagger添加JWT认证                                                 
       c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
       {
           Scheme = "bearer",
           BearerFormat = "JWT",
           Type = SecuritySchemeType.Http,
           Name = "Authorization",
           In = ParameterLocation.Header,
           Description = "Authorization:Bearer {your JWT token}<br/><b>授权地址:/Base_Manage/Home/SubmitLogin</b>",
       });
       c.AddSecurityRequirement(new OpenApiSecurityRequirement
       {
               {
                   new OpenApiSecurityScheme
                   {
                       Reference = new OpenApiReference
                       {
                           Type = ReferenceType.SecurityScheme,
                           Id = "Bearer"
                       }
                   },
                   new string[] { }
               }
       });
       // 为 Swagger JSON and UI设置xml文档注释路径
       //获取应用程序所在目录(绝对,不受工作目录影响,建议采用此方法获取路径)
       var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);
       var xmls = Directory.GetFiles(basePath, "*.xml");
       xmls.ForEach(aXml =>
       {
           c.IncludeXmlComments(aXml);
       });
   });
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
   app.UseSwagger()
   .UseSwaggerUI(c =>
   {
       c.SwaggerEndpoint("/swagger/v1/swagger.json", "1.0.0");
       c.RoutePrefix = string.Empty;
   })
}

点击程序->属性
在这里插入图片描述
在这里插入图片描述

©️2020 CSDN 皮肤主题: 猿与汪的秘密 设计师:上身试试 返回首页