如何加载默认视图而不是Swagger UI?

iqxoj9l9  于 2022-11-06  发布在  其他
关注(0)|答案(2)|浏览(160)

我正在使用.NET 6 Web API,并且已将Program.cs配置为在开发模式下使用Swagger UI。如何更改此配置,以便我仍然可以访问Swagger UI,但在默认情况下加载默认视图?
例如,当我运行我的项目时,它加载的是https://localhost:7004/swagger/index.html,而不是我想要加载https://localhost:7004,但仍然可以访问https://localhost:7004/swagger/index.html
当前程序.cs:

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseStaticFiles();
app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();
app.MapControllerRoute(
    name: "default",
    pattern: "{controller=ControllerName}/{action=Index}/{id?}");
yzuktlbb

yzuktlbb1#

打开项目的属性(屏幕截图如下-)

在“调试”部分下,单击“打开调试启动配置文件UI”。这将打开如下所示的UI-

删除文本“swagger”,将其保留为空,重新启动应用程序。这将导航到-

请注意,您仍然可以输入swagger链接的完整路径以导航到它-

zf2sa74q

zf2sa74q2#

打开launchsettings.json并删除启动URL

{
  "$schema": "https://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:41869",
      "sslPort": 44362
    }
  },
  "profiles": {
    "WebApplication2": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "launchUrl": "swagger", // remove this
      "applicationUrl": "https://localhost:7189;http://localhost:5189",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger", // remove this
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

相关问题