关于c#:launchSettings.json launchUrl无法使用” api / values”

launchSettings.json launchUrl doesn't work “api/values”

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

我阅读了此解决方案

如何更改默认控制器和在ASP.NET Core API中的操作?

如何将根目录重定向到Asp.Net Core 2.x中的swagger?

每个人都写相同的东西,但对我不起作用。

我的launchSetting.json文件是

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{ "$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/zh-cn/aspnet/core/fundamentals/routing?view=aspnetcore-2.2

这也不起作用。api/values是从哪里来的?我不知道。

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


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
{
 "$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运行项目时),因此可以更改为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
{
 "$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命令将始终始终为首尾网址提供服务。


我正在使用VSCode并在我身上发生同样的事情,但是在Windows上,我只是在这里增强了答案。

我已经尝试了VS2019,并且在该IDE中工作,所以应该是与VSCode有关。我已经搜索了其他答案,但发现了这个问题。

vscode launch.json debug and open specific url

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

1
2
3
4
5
6
7
        "launchBrowser": {
           "enabled": true,
           "args":"${auto-detect-url}",
           "windows": {
               "command":"cmd.exe",
               "args":"/C start ${auto-detect-url}/swagger"
            }

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


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