如何使用 Swashbuckle 在 Swagger 中设置操作名称

How to set the operation name in Swagger using Swashbuckle

我有一个 WebAPI,其操作类似于 "MyActionName" 路由设置为 Books\Read.

我有 XML 评论和 Swashbuckle 配置生成 SwaggerUI 帮助页面。

动作在 Swagger 中正确列出 UI,显示 Books\Read 作为动作的名称(例如)。

但是,基于 Swagger.json 的 codegen 会导致名为 "MyActionName" 的操作,这是 C# 代码中的原始名称。

如何将操作名称设置为其他名称,更像是路线(例如:BooksRead)?

您可以创建如下所示的操作过滤器:

public class CustomOperationFilter: IOperationFilter
    {        
        public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
        {            
            if (operation != null)
            {
                operation.operationId = "OperationName"; // Give whatever name that is required
            }
        }
    }

此外,在 swagger 配置文件中附加操作过滤器:

c.OperationFilter<CustomOperationFilter>();