How can i specify the default opening version of swagger?(如何指定 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 的默认打开版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何指定 swagger 的默认打开版本?


- 输入按键事件处理程序 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01