如何指定 swagger 的默认打开版本? [英] How can i specify the default opening version of swagger?

查看:68
本文介绍了如何指定 swagger 的默认打开版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 Swagger 作为 API 文档的 C# Web API.我使用了 Swashbuckle 包.swagger 环境正在使用我在控制器中指定的多个版本.

I have a C# web API that is using Swagger as API documentation. I have used the Swashbuckle packages. The swagger environment is working with multiple versions that i specify in the controllers.

今天我介绍了一个新的未来版本(1.2),它仍在开发中.我想默认在 1.1 版本上打开 swagger,但仍然在右上角的下拉列表中保持正确的排序顺序(例如 v1、v1.1、v1.2).目前它总是在下拉列表中打开顶级版本.

Today i introduced a new future version (1.2) that is still under development. I would like to open swagger on the version 1.1 version by default but still keep the correct sorting order in the dropdown in the top right (e.g. v1, v1.1, v1.2). Currently it always opens the top version in the drop down.

有人知道怎么做吗?

推荐答案

你在 Startup.cs - Configure 方法中配置 Swagger UI 的顺序决定了下拉列表的顺序.默认情况下,UI 会显示与下拉列表中的第一个选项对应的规范.

The order in which you configure Swagger UI in Startup.cs - Configure method determines the dropdown list order. By default, the UI displays the specifications corresponding to the first option in the dropdown.

我们可以更改版本的顺序,如下所示,但我不确定是否有任何属性可以覆盖默认版本,同时保留 UI 中的版本下拉顺序.

We can change the order of versions as shown below, but I am not sure if there is any property to override default version while retaining the dropdown order of versions in UI.

在下面的例子中,它会默认打开 v1.1.

In the below example, it will open v1.1 by default.

app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1.1/swagger.json", "V1.1");
    c.SwaggerEndpoint("/swagger/v1.0/swagger.json", "V1.0");
    c.SwaggerEndpoint("/swagger/v1.2/swagger.json", "V1.2");
}

但是,有一个解决方法:您可以传递查询字符串参数 urls.primaryName 的值,以便它默认加载该版本.

Howerver, there's a workaround: You can pass the value of querystring parameter urls.primaryName so that it loads that version by default.

https://localhost:5001/swagger/index.html?urls.primaryName=v1.1

(或)您可以尝试通过注入自定义 javascript 来自定义 Swagger UI,如下所示:

(Or) You can try customizing the Swagger UI by injecting custom javascript as follows:

app.UseSwaggerUI( 
            ....
            c => c.InjectJavascript(***pass custom javascript here***) )

这篇关于如何指定 swagger 的默认打开版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆