27

Given the following auto-generated Visual Studio Code launch.json config:

{
    "name": "Launch Demo.Api",
    "type": "coreclr",
    "request": "launch",
    "preLaunchTask": "build",
    "program": "${workspaceRoot}/Demo.Api/bin/Debug/netcoreapp2.1/Demo.Api.dll",
    "args": [],
    "cwd": "${workspaceRoot}/Demo.Api",
    "stopAtEntry": false,
    "launchBrowser": {
        "enabled": true,
        "args": "${auto-detect-url}",
        "windows": {
            "command": "cmd.exe",
            "args": "/C start ${auto-detect-url}"
        },
        "osx": {
            "command": "open"
        },
        "linux": {
            "command": "xdg-open"
        }
    },
    "env": {
        "ASPNETCORE_ENVIRONMENT": "Development"
    }
}

I'd like this to launch the browser to localhost:5000/swagger when I debug, but I've tried half a dozen different things and nothing works. It just opens to localhost:5000. What am I missing here? There's no general documentation (that I could find) on all of the attributes available aside from hitting Ctrl+space to see a list, which doesn't help much.

6 Answers 6

87

this one also works for me on VSCode 1.39.2

// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
"serverReadyAction": {
    "action": "openExternally",
    "pattern": "\\bNow listening on:\\s+(https?://\\S+)",
    "uriFormat": "%s/swagger"
}, 
Sign up to request clarification or add additional context in comments.

1 Comment

The accepted answer no longer solves the problem (SEP 2023), but this one does. I suggest @kizmar would kindly mark this as the proper answer.
11

This one works for me:

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

Comments

6

I tried the following and it seems to work

"launchBrowser": {
"enabled": true,
"args": "${auto-detect-url}/swagger",

1 Comment

This didn't work for me - windows 10, .NET 5.0 , vscode: 1.61.0
1

An alternative would be using VSCode 1.48 (July 2020), which adds a Debug: Open Link command:

A new Debug: Open Link command has been added to quickly debug any URL.

Previously, to debug a browser, you had to install the Debugger for Chrome extension and write a launch.json config file to debug a page.
This command allows you to debug any URL without needing additional launch configurations.

Debug URL

Theme: Earthsong

If you have a URL selected in your active editor, it will open that automatically.
Otherwise, VS Code will prompt you to enter a URL, pre-filling with the URL in your clipboard, if any.

You can adjust the debug configuration used in this command via the debug.javascript.debugByLinkOptions setting.

That last setting can be used for specifying the right certificate

By default we'll use a different user data dir per workspace.
You can use a "stable" directory by adding something like to your user settings:

"debug.javascript.debugByLinkOptions": {
  "userDataDir": "C:/Users/user/my-user-data-dur"
}

This will then allow any flags or settings you have to stick. I'm not sure which flags you need to fiddle with to make Chrome happy, but that config should let you set them in a way that won't be reset.

And VSCode 1.50 (Sept. 2020) will improve that feature:

  • Adding a button to launch it directly from the Run and debug tab (instead of having to open the Palette).
    Currently the Run and debug tab only proposes to create a launch.json file, or to use Node.js Debug Terminal, but now it would be simpler to propose the Open link feature directly.
  • It would also be great if, when the URL is filled, it is automatically saved in .vscode/settings.json, to avoid to have to fill it again every time.

See commit dc22997 as a result.

Comments

1

If you are debugging node project this worked for me.

launch.json in my front-end app (Angular)

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch via npm",
      "type": "node",
      "request": "launch",
      "cwd": "${workspaceFolder}",
      "runtimeExecutable": "npm",
      "runtimeArgs": [ "run-script", "start" ],
      "console": "externalTerminal"
    }
  ]
}

package.json that contains the npm scripts

In my case since I was debugging Angular using ng serve I had to specify the --open=true argument so the browser would launch

  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve --ssl --ssl-cert %APPDATA%\\ASP.NET\\https\\%npm_package_name%.pem --ssl-key %APPDATA%\\ASP.NET\\https\\%npm_package_name%.key --open=true",
    "build": "ng build",
    "watch": "ng build --watch --configuration development",
    "test": "ng test",
    "prestart": "node aspnetcore-https"
  },

Comments

0

So i had this issue trying to run a C# API on Linux and this is the solution that worked for me
in .vsocde/launch.json add this in the configuration property

"launchBrowser": {
    "enabled": true,
    // change this to your OS name, linux, osx or windows
    // you also need to change the command since xdg-open only works on linux
    "linux": {
        "command": "xdg-open",
        "args": "${auto-detect-url}/swagger/index.html"
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.