我使用的是Swagger UI 3.7.0附带的springfox-swagger-ui 2.8.0。
我想摆脱api文档页面前面的控制器列表,对我来说毫无用处(每个选项卡都是空的。
我已经尝试使用@ApiIgnore注释控制器类,但是当然,这也会删除我需要的其余api文档。
基本上,我想删除它:
同时保留此:
我浏览了在线文档,GitHub问题,StackOverflow问题,Google ...什么都没有。我是这个请求中唯一的吗?
将属性描述添加到@Api
:
例如:
@Api(value = "Test API Controller", produces = MediaType.APPLICATION_JSON_VALUE, tags = {"test-api-controller"}, description = "Testing API")
在控制器上尝试此属性
[ApiExplorerSettings(IgnoreApi = true)]
。NetCore以不同的方式实现此目的
SwaggerGroupAttribute
public class SwaggerGroup : Attribute
{
public SwaggerGroup(string groupName)
{
this.GroupName = groupName;
}
public string GroupName { get; }
}
SwaggerGen操作筛选器实现
using Swashbuckle.AspNetCore.SwaggerGen;
public class SwaggerOperationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
// Check at action level attribs first
var controllerActDesc = context.ApiDescription.ActionDescriptor as ControllerActionDescriptor;
if (controllerActDesc != null)
{
var swaggerGroupAttrib = context.MethodInfo.GetCustomAttributes(typeof(SwaggerGroup), true).FirstOrDefault() as SwaggerGroup;
// Check at Controller level attribs next
if (swaggerGroupAttrib == null)
{
swaggerGroupAttrib = controllerActDesc.ControllerTypeInfo.GetCustomAttributes(typeof(SwaggerGroup), true).FirstOrDefault() as SwaggerGroup;
}
if (swaggerGroupAttrib != null)
{
// This gets rid of default tags
operation.Tags.Clear();
operation.Tags.Add(new OpenApiTag() { Name = swaggerGroupAttrib.GroupName });
context.ApiDescription.GroupName = swaggerGroupAttrib.GroupName;
}
}
}
}
在方法startup.cs
中的ConfigureServices(IServiceCollection services)
中
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "FOO API", Version = "v1" });
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath, includeControllerXmlComments: true);
// Register operation filter here
c.OperationFilter<SwaggerOperationFilter>();
});