Skip to content

Commit b9cadc3

Browse files
authored
Merge pull request #214 from cd-linxi/master
swagger文档分组显示,swagger对控制器描述
2 parents cace150 + a95b24d commit b9cadc3

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
7+
namespace VOL.WebApi.Controllers.Order
8+
{
9+
/// <summary>
10+
/// 测试swagger对控制器的注释描述
11+
/// </summary>
12+
[ApiExplorerSettings(GroupName = "v2")]//Select a definition 下拉选项
13+
public class TestController : Controller
14+
{
15+
public IActionResult Index()
16+
{
17+
return View();
18+
}
19+
}
20+
}

Vue.Net/VOL.WebApi/Startup.cs

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
using Microsoft.Extensions.FileProviders;
1818
using Microsoft.Extensions.Hosting;
1919
using Microsoft.Extensions.Logging;
20+
using Microsoft.Extensions.PlatformAbstractions;
2021
using Microsoft.IdentityModel.Tokens;
2122
using Microsoft.OpenApi.Models;
2223
using Newtonsoft.Json;
24+
using Swashbuckle.AspNetCore.SwaggerGen;
2325
using VOL.Core.Configuration;
2426
using VOL.Core.Extensions;
2527
using VOL.Core.Filters;
@@ -117,9 +119,17 @@ public void ConfigureServices(IServiceCollection services)
117119
services.AddControllers();
118120
services.AddSwaggerGen(c =>
119121
{
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[] { } } };
123133
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
124134
{
125135
Description = "JWT授权token前面需要加上字段Bearer与一个空格,如Bearer token",
@@ -182,7 +192,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
182192
string _uploadPath = (env.ContentRootPath + "/Upload").ReplacePath();
183193

184194
if (!Directory.Exists(_uploadPath))
185-
{
195+
{
186196
Directory.CreateDirectory(_uploadPath);
187197
}
188198

@@ -205,8 +215,10 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
205215
app.UseSwagger();
206216
app.UseSwaggerUI(c =>
207217
{
218+
//2个下拉框选项 选择对应的文档
208219
c.SwaggerEndpoint("/swagger/v1/swagger.json", "VOL.Core后台Api");
209-
c.RoutePrefix = "";
220+
c.SwaggerEndpoint("/swagger/v2/swagger.json", "测试第三方Api");
221+
c.RoutePrefix = "";
210222
});
211223
app.UseRouting();
212224
//UseCors,UseAuthenticationg两个位置的顺序很重要
@@ -219,4 +231,25 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
219231
});
220232
}
221233
}
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+
}
222255
}

Vue.Net/VOL.WebApi/VOL.WebApi.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@
44
<TargetFramework>netcoreapp3.1</TargetFramework>
55
</PropertyGroup>
66

7+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
8+
<OutputPath>bin\Debug\netcoreapp3.1\</OutputPath>
9+
<DocumentationFile>bin\Debug\netcoreapp3.1\VOL.WebApi.xml</DocumentationFile>
10+
</PropertyGroup>
11+
712
<ItemGroup>
813
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="5.0.1" />
914
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.0" />
15+
<PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="1.1.0" />
1016
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc5" />
1117
</ItemGroup>
1218

Vue.Net/VOL.WebApi/VOL.WebApi.csproj.user

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
<PropertyGroup>
77
<ActiveDebugProfile>VOL.WebApi</ActiveDebugProfile>
88
<NameOfLastUsedPublishProfile>FolderProfile</NameOfLastUsedPublishProfile>
9+
<Controller_SelectedScaffolderID>MvcControllerEmptyScaffolder</Controller_SelectedScaffolderID>
10+
<Controller_SelectedScaffolderCategoryPath>root/Common/MVC/Controller</Controller_SelectedScaffolderCategoryPath>
911
</PropertyGroup>
1012
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
1113
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>

0 commit comments

Comments
 (0)