How can i specify the default opening version of swagger?

岁酱吖の 提交于 2020-02-16 05:27:51

问题


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.

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.

Does someone has an idea how to do this?


回答1:


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.

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.

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");
}

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

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

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



回答2:


Extending Bob's answer: the solution is to edit Properties/launchSettings.json:

"profiles": {
  "Command.Broker": {
    "commandName": "Project",
    "launchBrowser": true,
    "launchUrl": "swagger/index.html?urls.primaryName=v1.1",
    "environmentVariables": {
      "ASPNETCORE_ENVIRONMENT": "Development"
    },
    "applicationUrl": "http://localhost:51476/"
  },
}


来源:https://stackoverflow.com/questions/60186760/how-can-i-specify-the-default-opening-version-of-swagger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!