简体   繁体   中英

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.

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 (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. you could write your own OrderBy Extension if you wanted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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