运行应用程序时,在 VSCode 中启动 ASP.NET Core 应用程序中的特定 URL

Int*_*020 9 c# visual-studio-code asp.net-core

(如果重要的话,可以在 Mac 上运行。)

在 Visual Studio 中,您可以在运行或调试 ASP.NET Core 项目时使用此方法启动特定 URL。使用 launchUrl 属性。这是一个示例 launchSettings.json 文件:

{
  "profiles": {
    "MyProjectName": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "customstartupurlgoeshere/?id=theanswertotheuniverse",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:5001;http://localhost:5000"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

VS Code 中的模拟是什么?我读到 VS Code 会忽略 launchSettings.json 文件。

小智 10

您可以更新您的launch.json. 添加;serverReadyActionuriFormat例如,

 "serverReadyAction": {
     "action": "openExternally",
     "pattern": "\\bNow listening on:\\s+https://localhost:([0-9]+)",
     "uriFormat": "https://localhost:%s/swagger"
 },
Run Code Online (Sandbox Code Playgroud)

请注意,在示例中,我们专门捕获端口号,因为第一个端口号%s位于uriFormat.

  • 对于 .NET Core 6 只需添加 "uriFormat": "%s/swagger" 即可。 (7认同)

itm*_*nus 6

.vscode/launch.jsonVSCode在启动程序时获取配置。

使用 launchUrl 属性....VS Code 中的模拟是什么?

您可以更改args参数以接受 的参数--urls。例如,如果你想让 kestrel 监听6001/ 6000

“配置”:[
    {
        "name": ".NET Core 启动(网络)",
        “类型”:“coreclr”,
        “请求”:“启动”,
        "preLaunchTask": "构建",
        "程序": "${workspaceFolder}/bin/Debug/netcoreapp3.0/App.dll",
        "args": ["--urls","https://localhost:6001;http://localhost:6000"],
        "cwd": "${workspaceFolder}",
        “stopAtEntry”:假,
        “服务器就绪操作”:{
            "action": "外部开放",
            "pattern": "^\\s*正在收听:\\s+(https?://\\S+)"
        },
        “环境”:{
            "ASPNETCORE_ENVIRONMENT": "开发"
        },
        “源文件映射”:{
            "/Views": "${workspaceFolder}/Views"
        }
    },

  • 这不是OP提出的问题的答案,这个答案是如何设置“launchSettings.json”的“applicationUrl”属性而不是“launchUrl”的等效项 (6认同)

pus*_*ack 5

对于 .NET core 6、7 和 8,编辑 .vscode/launch.json 并添加"uriFormat": "%s/swagger"如下内容:

"serverReadyAction": {
  "action": "openExternally",
  "pattern": "\\bNow listening on:\\s+(https?://\\S+)",
  "uriFormat": "%s/swagger"
}
Run Code Online (Sandbox Code Playgroud)