launchSettings.json launchUrl 不起作用"api/values"

launchSettings.json launchUrl doesn't work "api/values"

我正在尝试更改 http://localhost:5001/api/values 路线,但程序卡在了这个 url。

我阅读了这个解决方案

https://medium.com/quick-code/routing-in-asp-net-core-c433bff3f1a4

每个人都写同样的东西,但不适合我。

我的 launchSetting.json 文件是

{  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:54650",
      "sslPort": 44382
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "ShoppingBasketAPI": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

我试过改app.UseMvc();

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-2.2

这也不是working.Whereapi/values从何而来?我想不通。

我的控制器属性路由是 [Route("api/[controller]/[action]")]

当您创建新的 ASP.Net Core Web API 项目时,您将在项目 属性 中看到 Launch browser 设置为 api/values 路径.因此,您可以将其更改为您想要的 url 或者您可以在 launchSetting.json 文件

中进行更改
{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false, 
    "anonymousAuthentication": true, 
    "iisExpress": {
      "applicationUrl": "http://localhost:54356",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "WebApplication4": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "applicationUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

因此您将在配置文件部分看到将有 2 个配置。一个用于 IIS express(当使用 Visual Studio 到 运行 你的代码时)和 WebApplication4(当你 运行 使用 dotnet 运行 项目时)所以你可以更改为

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false, 
    "anonymousAuthentication": true, 
    "iisExpress": {
      "applicationUrl": "http://localhost:54356",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "WebApplication4": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "applicationUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

因此,当您使用 VS 运行 项目或 dotnet run 命令时,将始终首先为 swagger url 服务。

问题是因为 Visual Studio for Mac。我能够得到正确的 url 机会 Run With>Custom Configuration > Run Action --> Debug .Net Core Debugger

我正在使用 VSCode,我也遇到了同样的情况,但在 Windows 上。 我只是在这里加强了一点答案。

我已经尝试过 VS2019 并且它在 IDE 中工作,所以它应该与 VSCode 有关。 我搜索了其他答案并找到了这个。

解决我的问题的方法是转到 .vscode/launch.json 文件并附加:

         "launchBrowser": {
            "enabled": true,
            "args": "${auto-detect-url}",
            "windows": {
                "command": "cmd.exe",
                "args": "/C start ${auto-detect-url}/swagger"
            }

如 Anton Dremin 所述,只需将以上内容添加到配置部分即可。