How to set not to open new web browser tab in vscode when running asp.net core api project

Viewed 2802

e.g

Every time i key ctrl + f5 or ctrl + shift + f5 to rerun asp.net core api project in vscode,it'll open a new tab.

I expect just restart program without opening new tab.

3 Answers

updated : CLI version (recommand)

It can use dotnet watch to do it .

visul studio code (op want to merge)

If you don't want any browser tab be created,you colud go to .vscode\launch.json remove serverReadyAction and it can deal the problem.

default json :

{
   // Use IntelliSense to find out which attributes exist for C# debugging
   // Use hover for the description of the existing attributes
   // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
   "version": "0.2.0",
   "configurations": [
        {
            //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+)"
            }
        }
    ]
}

just delete or mark serverReadyAction then system'll not open any web browerser tab

{
   // Use IntelliSense to find out which attributes exist for C# debugging
   // Use hover for the description of the existing attributes
   // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
   "version": "0.2.0",
   "configurations": [
        {
            //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+)"
            //}
        }
    ]
}


P.S

I test set launchBrowser enabled false but it not work,it'll still create new tab.

launchBrowser enabled false launch.json :

{
   // Use IntelliSense to find out which attributes exist for C# debugging
   // Use hover for the description of the existing attributes
   // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
   "version": "0.2.0",
   "configurations": [
        {
            "launchBrowser": {
                "enabled": false    
            },            
            "name": ".NET Core Launch (web)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            // If you have changed target frameworks, make sure to update the program path.
            "program": "${workspaceFolder}/bin/Debug/netcoreapp3.0/ServerApp.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            "stopAtEntry": false,
            //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+)"
            },
            "env": {
                "ASPNETCORE_ENVIRONMENT": "Development"
            },
            "sourceFileMap": {
                "/Views": "${workspaceFolder}/Views"
            }
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach",
            "processId": "${command:pickProcess}"
        }
    ]
}

Visual Studio Version :

Every kind of application shows you some kind of UI, prompt, or form. Otherwise, how would user know that’s its up and running.

By default, it’s open a new tab only.

Your project template itself is web API? then how can you expect the web without the browser?

Update: I tried setting the launchBrowser to false in launchSettings.json in Visual studio 2019, it’s launching like the below images.

"profiles": {
  "IIS Express": {
    "commandName": "IISExpress",
    "launchBrowser": false,
    "launchUrl": "default",
    "environmentVariables": {
      "ASPNETCORE_ENVIRONMENT": "Development"
    }
  }

You are setting launchBrowser to false in wrong way, you have to do like this "launchBrowser": false. Its working fine in Asp.Net Core 3.1 API.

You can see in the taskbar

enter image description here enter image description here

If you don't want any browser tab be created,you colud go to .vscode\launch.json remove serverReadyAction and it can deal the problem.

default json :

{
   // Use IntelliSense to find out which attributes exist for C# debugging
   // Use hover for the description of the existing attributes
   // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
   "version": "0.2.0",
   "configurations": [
        {
            //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+)"
            }
        }
    ]
}

just delete or mark serverReadyAction then system'll not open any web browerser tab

{
   // Use IntelliSense to find out which attributes exist for C# debugging
   // Use hover for the description of the existing attributes
   // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
   "version": "0.2.0",
   "configurations": [
        {
            //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+)"
            //}
        }
    ]
}


P.S

I test set launchBrowser enabled false but it not work,it'll still create new tab.

launchBrowser enabled false launch.json :

{
   // Use IntelliSense to find out which attributes exist for C# debugging
   // Use hover for the description of the existing attributes
   // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
   "version": "0.2.0",
   "configurations": [
        {
            "launchBrowser": {
                "enabled": false    
            },            
            "name": ".NET Core Launch (web)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            // If you have changed target frameworks, make sure to update the program path.
            "program": "${workspaceFolder}/bin/Debug/netcoreapp3.0/ServerApp.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            "stopAtEntry": false,
            //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+)"
            },
            "env": {
                "ASPNETCORE_ENVIRONMENT": "Development"
            },
            "sourceFileMap": {
                "/Views": "${workspaceFolder}/Views"
            }
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach",
            "processId": "${command:pickProcess}"
        }
    ]
}

after setting for vscode files and add to launch.json

{
  "name": ".NET Core Launch (web)",
  "type": "coreclr",
  "request": "launch",
  "preLaunchTask": "build",
  "program": "${workspaceFolder}/Parto.Web/bin/Debug/net5.0/Parto.Web.dll",
  "args": [],
  "cwd": "${workspaceFolder}/Parto.Web",
  "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"
    }
  },
  "serverReadyAction": {
    "action": "openExternally",
    "pattern": "\\bNow listening on:\\s+(https?://\\S+)"
  },
  "env": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  },
  "sourceFileMap": {
    "/Views": "${workspaceFolder}/Views"
  }
},
{
        "name": ".NET Core Launch - Chrome",
        "type": "coreclr",
        "request": "launch",
        "preLaunchTask": "build",
        "program": "${workspaceFolder}/Parto.Web/bin/Debug/net5.0/Parto.Web.dll",
        "args": [],
        "cwd": "${workspaceFolder}/Parto.Web",
        "stopAtEntry": false,
        "internalConsoleOptions": "openOnSessionStart",
        "launchBrowser": {
            "enabled": true,
            "args": "${auto-detect-url}",
            "windows": {
                "command": "cmd.exe",
                "args": "/C start \"\" \"C:/Program Files/Google/Chrome/Application/chrome.exe\" ${auto-detect-url}"
            }
        },
        "env": {
            "ASPNETCORE_ENVIRONMENT": "Development",
            "ASPNETCORE_URLS": "http://localhost:5000"
        },
        "sourceFileMap": {
            "/Views": "${workspaceFolder}/Parto.Web/Views"
        }
},

and settings.json

{
"omnisharp.path": "latest",
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[csharp]": {
  "editor.defaultFormatter": "ms-dotnettools.csharp"
}

}

and add this to omnisharp.json

"MSBuild": {
"UseLegacySdkResolver": true}

in VS Code terminal

First dotnet build

Second dotnet run "Address of Web Project"

then after run project successfully

Launching browser by cmd.exe /C start http://localhost:5000

Related