如何将 swagger 设置为起始页 https://localhost:44321/swagger/index.html 改为 https://localhost:44321/swagger 设置为默认页面?
这是我的代码
IApplicationBuilder
:
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.UseSwagger();
app.UseSwaggerUI(c =>
{
string clientId = Configuration["Swagger:ClientId"];
c.SwaggerEndpoint("/swagger/v1/swagger.json", "aims_api v1");
c.OAuthClientId(clientId);
c.OAuthAppName("Azure AD API");
c.OAuthScopeSeparator(" ");
});
在
launchSettings.json
中将 launchUrl
更改为招摇 :
"ProjectName": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
如果你想自定义默认的 swagger url :
更改
RoutePrefix
:
app.UseSwaggerUI(c =>
{
//...
c.RoutePrefix = "myapi/swagger";
});
并更改
launchSettings.json
:
"ProjectName": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"launchUrl": "myapi/swagger",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
如果您从服务器下载发布配置文件(Alt+;,Alt+R),您会发现一个名为“SiteUrlToLaunchAfterPublish”的标签
我的看起来像这样
<SiteUrlToLaunchAfterPublish>https://your-app-service.azurewebsites.net/swagger</SiteUrlToLaunchAfterPublish>
希望有帮助。