【问题标题】:Sort on controller name or tag with NSwag and C#使用 NSwag 和 C# 对控制器名称或标签进行排序
【发布时间】:2018-12-07 08:59:55
【问题描述】:

我有一个 ASP.Net Core 解决方案,我想使用 Swagger。为此,我使用 Nuget 包 NSwag(Assembly NSwag.AspNetCore,Version=11.20.1.0)。在我的应用程序配置中,我有以下内容:

public void Configure(IApplicationBuilder app) {
    app..UseSwaggerUi3WithApiExplorer(settings =>
        {
            settings.GeneratorSettings.DefaultPropertyNameHandling = PropertyNameHandling.CamelCase;
            settings.PostProcess = document =>
            {
                document.Info.Version = $"v{typeof(Startup).Assembly.GetName().Version.Major}";
                document.Info.Title = "Test Api";
                document.Info.Description = "Sample API";
                document.Info.TermsOfService = "None";
                document.Info.Contact = new NSwag.SwaggerContact
                {
                    Name = "Person",
                    Email = "Email"
                };
            };
        });
}

当我运行它时,我正确地看到了所有控制器和相应的方法,但它没有按字母顺序排序。

我已经尝试了以下方法:

  • 将 TagSorter 添加到设置中:

    app.UseSwaggerUi3WithApiExplorer(settings =>
        {
            ...
            settings.TagSorter = "alpha";
            ...
        });
    
  • 将 ApisSorter 添加到设置中:

    app.UseSwaggerUi3WithApiExplorer(settings =>
        {
            ...
            settings.ApisSorter = "alpha";
            ...
        });
    

但这些更改会产生相同的输出。如何实现排序?

【问题讨论】:

    标签: c# swagger-ui nswag


    【解决方案1】:

    我有同样的问题。但是在升级 NSwag.AspNetCore -> 12.0.8(撰写本文时的最新版本)并替换现在的 Obsolete 方法后,它可以使用

    s.TagsSorter = "alpha";
    s.OperationsSorter = "alpha";
    

    【讨论】:

    • 当我升级时,我在运行我的项目时遇到错误,我不能使用 UseSwaggerUi3WithApiExplorer(),而是使用 UseSwaggerUi3(),然后当我转到我的 Swagger 页面时,我收到错误:无法加载 API 定义。
    • 嗯,我用app.UseSwaggerUi3WithApiExplorer(...)UseSwaggerUi3 被标记为已过时,并带有注释使用 UseSwaggerUi3WithApiExplorer 代替。 编辑:没关系,UseSwaggerUi3 有一个重载并没有标记为过时跨度>
    猜你喜欢
    • 2020-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    • 2014-03-23
    • 1970-01-01
    • 2021-08-19
    相关资源
    最近更新 更多