如何更改操作在 Swashbuckle 中的组中列出的顺序? [英] How can I change order the operations are listed in a group in Swashbuckle?
问题描述
我正在使用 Swashbuckle 生成 Swagger UI.它具有用于选择分组键(默认为控制器)和组排序的选项,但我想为组中的操作选择一个顺序,以便 GET
始终出现在 之前例如删除
.
I'm using Swashbuckle to generate Swagger UI. It has options for choosing a grouping key (controller by default) and the ordering of the groups, but I would like to choose an order for the operations in a group so that GET
appears always before DELETE
for example.
我已经找到了如何实现文档过滤器,我可以通过 HttpMethod
获取和订购 ApiDescriptions
,但是更改 ApiDescriptions
中的顺序不会没有反映在生成的 Swagger UI 中,我找不到如何在 swaggerDoc
中保留订单.
I've found how to implement document filters and I can get and order ApiDescriptions
by HttpMethod
, but changing the order in ApiDescriptions
doesn't reflect in the generated Swagger UI and I can't find how to persist the order in swaggerDoc
.
SwaggerDocument
有一个 paths
属性,但其中的 PathItem
将每个 HTTP 方法作为一个属性,所以我不知道如何为他们选择演示顺序.然而,当我的 API 的 Swagger UI 生成时,不同的控制器在页面中获得不同的方法顺序.
SwaggerDocument
has a paths
property, but the PathItem
in it has each HTTP method as a property, so I can't figure how to choose an order of presentation for them. Eventough, when the Swagger UI for my API is generated, different controllers get different method order in the page.
我应该手动重新排序控制器中的方法实现吗?
Should I manually reorder the methods implementation in my controller instead?
推荐答案
你见过这个问题吗?https://github.com/domaindrivendev/Swashbuckle/issues/11
public class CustomDocumentFilter : IDocumentFilter
{
public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, System.Web.Http.Description.IApiExplorer apiExplorer)
{
//make operations alphabetic
var paths = swaggerDoc.Paths.OrderBy(e => e.Key).ToList();
swaggerDoc.Paths = paths.ToDictionary(e => e.Key, e => e.Value);
}
}
然后:
c.DocumentFilter<CustomDocumentFilter>();
这篇关于如何更改操作在 Swashbuckle 中的组中列出的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!