.net 如何在dotnet内核中设置swagger为起始页

sqxo8psd  于 2023-01-22  发布在  .NET
关注(0)|答案(2)|浏览(306)

如何将swagger设置为起始页https://localhost:44321/swagger/index.html设置为默认页?
这是我在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(" ");               

        });
nqwrtyyt

nqwrtyyt1#

launchSettings.json中,将launchUrl更改为摆动:

"ProjectName": {
    "commandName": "Project",
     "dotnetRunMessages": "true",
     "launchBrowser": true,
     "launchUrl": "swagger",
     "applicationUrl": "https://localhost:5001;http://localhost:5000",
     "environmentVariables": {
       "ASPNETCORE_ENVIRONMENT": "Development"
     }
}

如果你想自定义默认的swagger网址:
更改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"
     }
}
nmpmafwu

nmpmafwu2#

如果从服务器下载发布配置文件(Alt+;,Alt+R),您将找到一个名为“SiteUrlToLaunchAfterPublish”的标记
我的看起来像<SiteUrlToLaunchAfterPublish>https://your-app-service.azurewebsites.net/swagger</SiteUrlToLaunchAfterPublish>
希望能有所帮助。

相关问题