17
17
using Microsoft . Extensions . FileProviders ;
18
18
using Microsoft . Extensions . Hosting ;
19
19
using Microsoft . Extensions . Logging ;
20
+ using Microsoft . Extensions . PlatformAbstractions ;
20
21
using Microsoft . IdentityModel . Tokens ;
21
22
using Microsoft . OpenApi . Models ;
22
23
using Newtonsoft . Json ;
24
+ using Swashbuckle . AspNetCore . SwaggerGen ;
23
25
using VOL . Core . Configuration ;
24
26
using VOL . Core . Extensions ;
25
27
using VOL . Core . Filters ;
@@ -117,9 +119,17 @@ public void ConfigureServices(IServiceCollection services)
117
119
services . AddControllers ( ) ;
118
120
services . AddSwaggerGen ( c =>
119
121
{
120
- c . SwaggerDoc ( "v1" , new OpenApiInfo { Title = "VOL.Core后台Api" , Version = "v1" } ) ;
121
- var security = new Dictionary < string , IEnumerable < string > >
122
- { { AppSetting . Secret . Issuer , new string [ ] { } } } ;
122
+ //分为2份接口文档
123
+ c . SwaggerDoc ( "v1" , new OpenApiInfo { Title = "VOL.Core后台Api" , Version = "v1" , Description = "这是对文档的描述。。" } ) ;
124
+ c . SwaggerDoc ( "v2" , new OpenApiInfo { Title = "VOL.Core对外三方Api" , Version = "v2" , Description = "xxx接口文档" } ) ; //控制器里使用[ApiExplorerSettings(GroupName = "v2")]
125
+ //启用中文注释功能
126
+ var basePath = PlatformServices . Default . Application . ApplicationBasePath ;
127
+ var xmlPath = Path . Combine ( basePath , "VOL.WebApi.xml" ) ;
128
+ c . IncludeXmlComments ( xmlPath , true ) ; //显示控制器xml注释内容
129
+ //添加过滤器 可自定义添加对控制器的注释描述
130
+ //c.DocumentFilter<SwaggerDocTag>();
131
+
132
+ var security = new Dictionary < string , IEnumerable < string > > { { AppSetting . Secret . Issuer , new string [ ] { } } } ;
123
133
c . AddSecurityDefinition ( "Bearer" , new OpenApiSecurityScheme ( )
124
134
{
125
135
Description = "JWT授权token前面需要加上字段Bearer与一个空格,如Bearer token" ,
@@ -182,7 +192,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
182
192
string _uploadPath = ( env . ContentRootPath + "/Upload" ) . ReplacePath ( ) ;
183
193
184
194
if ( ! Directory . Exists ( _uploadPath ) )
185
- {
195
+ {
186
196
Directory . CreateDirectory ( _uploadPath ) ;
187
197
}
188
198
@@ -205,8 +215,10 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
205
215
app . UseSwagger ( ) ;
206
216
app . UseSwaggerUI ( c =>
207
217
{
218
+ //2个下拉框选项 选择对应的文档
208
219
c . SwaggerEndpoint ( "/swagger/v1/swagger.json" , "VOL.Core后台Api" ) ;
209
- c . RoutePrefix = "" ;
220
+ c . SwaggerEndpoint ( "/swagger/v2/swagger.json" , "测试第三方Api" ) ;
221
+ c . RoutePrefix = "" ;
210
222
} ) ;
211
223
app . UseRouting ( ) ;
212
224
//UseCors,UseAuthenticationg两个位置的顺序很重要
@@ -219,4 +231,25 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
219
231
} ) ;
220
232
}
221
233
}
234
+
235
+ /// <summary>
236
+ /// Swagger注释帮助类
237
+ /// </summary>
238
+ public class SwaggerDocTag : IDocumentFilter
239
+ {
240
+ /// <summary>
241
+ /// 添加附加注释
242
+ /// </summary>
243
+ /// <param name="swaggerDoc"></param>
244
+ /// <param name="context"></param>
245
+ public void Apply ( OpenApiDocument swaggerDoc , DocumentFilterContext context )
246
+ {
247
+ //添加对应的控制器描述
248
+ swaggerDoc . Tags = new List < OpenApiTag >
249
+ {
250
+ new OpenApiTag { Name = "Test" , Description = "这是描述" } ,
251
+ //new OpenApiTag { Name = "你的控制器名字,不带Controller", Description = "控制器描述" },
252
+ } ;
253
+ }
254
+ }
222
255
}
0 commit comments