【问题标题】:launchSettings.json launchUrl doesn't work "api/values"launchSettings.json launchUrl 不起作用“api/values”
【发布时间】:2019-09-23 14:03:35
【问题描述】:

我正在尝试更改http://localhost:5001/api/values 路由,但程序卡在此网址。

我阅读了这个解决方案

How to change the default controller and action in ASP.NET Core API?

How to redirect root to swagger in Asp.Net Core 2.x?

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

这也不行。api/values 来自哪里?我想不通。

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

【问题讨论】:

    标签: c# asp.net asp.net-core asp.net-core-webapi .net-core-2.2


    【解决方案1】:

    我正在使用 VSCode,并且在我身上发生了同样的情况,但在 Windows 上。 我只是在这里加强一点答案。

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

    vscode launch.json debug and open specific url

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

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

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

    【讨论】:

      【解决方案2】:

      问题在于 Visual Studio for Mac。我能够获得正确的 URL 机会Run With>Custom Configuration > Run Action --> Debug .Net Core Debugger

      【讨论】:

        【解决方案3】:

        当您创建新的 ASP.Net Core Web API 项目时,您会看到在项目属性中有 Launch browser 设置为 api/values 路径。因此,您可以将其更改为您想要的任何网址,也可以在您的 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 run 运行项目时),因此您可以更改为

        {
          "$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。

        【讨论】:

        • 是的,但这对他们俩都不起作用。我仍然得到 api/values 路由。可能是因为我使用 macOS 吗?
        • @HaktanEnesBiçer 我不这么认为。尝试在项目中搜索 api/values 以查看是否有任何代码包含它?我的方法工作得很好。我认为代码有问题
        • 我发现了问题。这是因为visual studio mac。我在'api/values'之前搜索它找不到任何代码。
        猜你喜欢
        • 1970-01-01
        • 2022-07-07
        • 1970-01-01
        • 1970-01-01
        • 2013-09-15
        • 1970-01-01
        • 1970-01-01
        • 2011-07-21
        • 1970-01-01
        相关资源
        最近更新 更多