我有一个C# ASP.NET WebAPI应用程序,它使用Swashbuckle自动生成API文档。我希望能够从文档中省略某些方法,但我似乎不知道如何告诉Swagger不要将它们包含在Swagger输出中。
我感觉到这与添加模型或模式筛选器有关,但不清楚该做什么,文档似乎只提供了如何为方法修改输出的示例,而不是从输出中完全删除它。
发布于 2019-02-20 14:39:51
做一个过滤器
public class SwaggerTagFilter : IDocumentFilter
{
public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
{
foreach(var contextApiDescription in context.ApiDescriptions)
{
var actionDescriptor = (ControllerActionDescriptor)contextApiDescription.ActionDescriptor;
if(!actionDescriptor.ControllerTypeInfo.GetCustomAttributes<SwaggerTagAttribute>().Any() &&
!actionDescriptor.MethodInfo.GetCustomAttributes<SwaggerTagAttribute>().Any())
{
var key = "/" + contextApiDescription.RelativePath.TrimEnd('/');
swaggerDoc.Paths.Remove(key);
}
}
}
}创建一个属性
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public class SwaggerTagAttribute : Attribute
{
}应用于startup.cs
services.AddSwaggerGen(c => {
c.SwaggerDoc(1, new Info { Title = "API_NAME", Version = "API_VERSION" });
c.DocumentFilter<SwaggerTagFilter>(); // [SwaggerTag]
});将SwaggerTag属性添加到您希望包含在Swagger中的方法和控制器中
https://stackoverflow.com/questions/29701573
复制相似问题