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

查看:556
本文介绍了我该如何指定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中配置Swagger UI的顺序-Configure方法确定下拉列表的顺序.默认情况下,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");
}

但是,有一种解决方法:您可以传递querystring参数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天全站免登陆