簡體   English   中英

運行asp.net核心api項目時如何在vscode中設置不打開新的web瀏覽器選項卡

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

例如

每次我在 vscode 中按ctrl + f5ctrl + shift + f5重新運行 asp.net core api 項目時,它都會打開一個新選項卡。

我希望在without opening new tab的情況下重新啟動程序。

更新:CLI 版本(推薦)

它可以使用dotnet watch來完成。

視覺工作室代碼(操作要合並)

如果您不想創建任何瀏覽器選項卡,請將 go 設置為.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然后系統將不會打開任何 web 瀏覽器選項卡

{
   // 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但它不起作用,它仍然會創建新標簽。

啟動瀏覽器啟用錯誤啟動。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? 那么你怎么能指望沒有瀏覽器的 web 呢?

更新:我嘗試在 Visual Studio 2019 的 launchSettings.json launchBrowser設置為false ,它的啟動方式如下圖所示。

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

您以錯誤的方式將launchBrowser設置為false ,您必須這樣做"launchBrowser": false 它在 Asp.Net Core 3.1 API 中運行良好。

你可以在任務欄中看到

在此處輸入圖像描述 在此處輸入圖像描述

如果您不想創建任何瀏覽器選項卡,請將 go 設置為.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然后系統將不會打開任何 web 瀏覽器選項卡

{
   // 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但它不起作用,它仍然會創建新標簽。

啟動瀏覽器啟用錯誤啟動。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}"
        }
    ]
}

在設置 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"
        }
},

和設置。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 啟動瀏覽器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM