简体   繁体   English

在swagger ui的选择下拉列表中将最新版本设置为默认值

[英]Set the latest version as default value in the select dropdown of swagger ui

I am using swagger Swashbuckle.AspNetCore latest version with .DotNetCore 2.1 api project which works fine. 我正在使用带有.DotNetCore 2.1 api项目的swagger Swashbuckle.AspNetCore最新版本,效果很好。

Is there a way to set the default selected value with the latest version. 有没有办法用最新版本设置默认选择的值。 Now, I am using for loop to get the last value and set it, 现在,我使用for循环来获取最后一个值并进行设置,

for (var i = provider.ApiVersionDescriptions.Count - 1; i >= 0; i--)
{
    var description = provider.ApiVersionDescriptions[i];
    options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName);
}

I'm not sure if there is a better way but you could use this: 我不确定是否有更好的方法,但是您可以使用以下方法:

foreach ( var description in provider.ApiVersionDescriptions.OrderByDescending(x=>x.ApiVersion.MajorVersion.GetValueOrDefault()) )
{
    options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName);
} 

Obviously you would need to add another OrderBy if you had something more than just MajorVersion changes. 显然,如果您所做的不仅是MajorVersion更改,还需要添加另一个OrderBy。 you could write your own OrderBy Extension if you wanted. 您可以根据需要编写自己的OrderBy扩展程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM