launchSettings.json launchUrl无法使用“ api / values”

时间:2019-05-05 21:43:00

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

我正在尝试更改http://localhost:5001/api/values路由,但该程序被卡在该URL中。

我阅读了此解决方案

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]")]

3 个答案:

答案 0 :(得分:0)

创建新的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网址。

答案 1 :(得分:0)

问题是由于Mac的Visual Studio。我能够获得正确的网址来获得机会Run With>Custom Configuration > Run Action --> Debug .Net Core Debugger

答案 2 :(得分:0)

我正在使用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)所述,只需将以上内容添加到“配置”部分。