前面已经介绍过了Swagger的基础使用了
下面继续分别详细说明下 不添加版本控制以及添加版本控制的使用情况,其实也基本一致,对看起来可能更加容易理解
第一步 导入nuget包
nuget导入Swashbuckle.AspNetCore (对应有Swashbuckle.AspNetCore.Swagger、Swashbuckle.AspNetCore.SwaggerGen、Swashbuckle.AspNetCore.SwaggerUI)
版本控制还需要引入
Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer
Microsoft.AspNetCore.Mvc.Versioning
第二步 添加服务及配置
下面代码中都结合了IdentityServer4中的相关设置,可以忽略
非版本控制
ConfigServices中添加如下代码
services.AddSwaggerGen(options =>{
options.SwaggerDoc("v1", newSwashbuckle.AspNetCore.Swagger.Info
{
Version= "v1.0",
Title= "体检微服务接口"});var basePath =PlatformServices.Default.Application.ApplicationBasePath;var xmlPath = Path.Combine(basePath, "ExaminationServicesApi.xml");
options.IncludeXmlComments(xmlPath);var xmlmodelPath =Path.Combine(basePath,"ExaminationServicesDomain.xml");
options.IncludeXmlComments(xmlmodelPath);#region Swagger添加授权验证服务
//options.AddSecurityDefinition("Bearer", new ApiKeyScheme//{//Description = "JWT Bearer 授权 \"Authorization: Bearer+空格+token\"",//Name = "Authorization",//In = "header",//Type = "apiKey"//});
options.AddSecurityDefinition("oauth2", newOAuth2Scheme
{
Type= "oauth2",
Flow= "implicit",
AuthorizationUrl= _authorityconfig.Authority + "/connect/authorize",
Scopes= new Dictionary{//{ "openid", "身份信息" } ,//{ "profile", &#