6

例如

每次我在 vscode 中键入ctrl + f5ctrl + shift + f5重新运行 asp.net core api 项目时,它都会打开一个新选项卡。

我希望只是重新启动程序without opening new tab

4

3 回答 3

9

更新:CLI 版本(推荐)

它可以dotnet watch用来做它。

视觉工作室代码(操作要合并)

如果您不想创建任何浏览器选项卡,您可以去.vscode\launch.json删除serverReadyAction它可以解决问题。

默认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+)"
            }
        }
    ]
}

只需删除或标记serverReadyAction然后系统将不会打开任何网络浏览器选项卡

{
   // 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+)"
            //}
        }
    ]
}


附言

我测试集launchBrowser enabled false但它不起作用,它仍然会创建新标签。

launchBrowser 启用了错误的 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}"
        }
    ]
}

视觉工作室版本:

每种应用程序都会向您显示某种 UI、提示或表单。否则,用户怎么会知道它已经启动并正在运行。

默认情况下,它只打开一个新标签。

您的项目模板本身就是 Web API?那么你怎么能指望没有浏览器的网络呢?

更新:我尝试在 Visual Studio 2019 的 launchSettings.json 中设置为launchBrowserfalse它的启动如下图所示。

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

你设置错误,你必须这样launchBrowser做。它在 Asp.Net Core 3.1 API 中运行良好。false"launchBrowser": false

你可以在任务栏中看到

在此处输入图像描述 在此处输入图像描述

于 2020-07-15T01:57:18.530 回答
3

如果您不想创建任何浏览器选项卡,您可以去.vscode\launch.json删除serverReadyAction它可以解决问题。

默认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+)"
            }
        }
    ]
}

只需删除或标记serverReadyAction然后系统将不会打开任何网络浏览器选项卡

{
   // 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+)"
            //}
        }
    ]
}


附言

我测试集launchBrowser enabled false但它不起作用,它仍然会创建新标签。

launchBrowser 启用了错误的 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}"
        }
    ]
}
于 2020-07-15T02:10:28.223 回答
0

在设置 vscode 文件并添加到 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"
        }
},

和 settings.json

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

}

并将其添加到omnisharp.json

"MSBuild": {
"UseLegacySdkResolver": true}

在 VS Code 终端中

第一个 dotnet 构建

第二个 dotnet 运行“Web 项目地址”

然后在成功运行项目后

通过 cmd.exe /C 启动浏览器 start http://localhost:5000

于 2021-11-23T12:58:42.117 回答