Asp.net-Core

如何使用 Visual Studio ASP.NET Core 監視文件更改“dotnet watch”

  • October 20, 2016

我正在使用帶有 ASP.NET Core 的 Visual Studio,並僅使用 F5 或 Ctrl+F5 執行網站(不直接使用命令行)。我想使用“dotnet watch”功能來確保所有更改都被即時獲取,以避免再次啟動伺服器。似乎使用命令行您會為此使用“dotnet watch run”,但Visual Studio 使用launchSettings.json 並在我理解正確的情況下在幕後進行。

我怎樣才能在那裡連接“dotnet watch”?

打開 launchSettings.json 並將其添加到profiles.

 "Watch": {
   "executablePath": "C:\\Program Files\\dotnet\\dotnet.exe",
   "commandLineArgs": "watch run",
   "launchBrowser": true,
   "launchUrl": "http://localhost:5000",
   "environmentVariables": {
     "ASPNETCORE_ENVIRONMENT": "Development"
   }
 }

打開 project.json 並將其添加到tools.

"Microsoft.DotNet.Watcher.Tools": "1.0.0-preview2-final"

恢復後,我們可以在 Visual Studio 中觀看。

在此處輸入圖像描述

如果您想使用 ASP.NET 2.x 或 3.x,您需要對其進行一些更改。

  • 監視工具現在是一個全域工具,您不再需要將其添加為參考
  • 語法略有不同
"Watch": {
 "executablePath": "dotnet.exe",
 "workingDirectory": "$(ProjectDir)",
 "commandLineArgs": "watch run",
 "launchBrowser": true,
 "launchUrl": "http://localhost:5000/",
 "environmentVariables": {
   "ASPNETCORE_ENVIRONMENT": "Development"
 }
}

對於 .Net 5 和 6

在 VisualStudio 2019 中

  1. 轉到工具 > ⚙ 選項 > 項目和解決方案 > ASP .NET Core
  2. 在 Auto build and refresh 選項中保存更改後選擇 Auto build and refresh browser
  3. 按 Ctrl + F5(不帶調試啟動)重要提示:僅在不帶調試的情況下執行時有效

否則將其添加到您的launchSettings.json

{
 "iisSettings": {
   ...
 },
 "profiles": {
   ... ,

   "Watch": {
     "commandName": "Executable",
     "executablePath": "dotnet.exe",
     "workingDirectory": "$(ProjectDir)",
     "commandLineArgs": "watch run"
   }

 }
}

自動生成profile的 with具有所需的"commandName":"Project"所有其他屬性:launchBrowserapplicationUrlenvironmentVariables和. 應該在那裡進行任何修改。dotnetRunMessages``hotReloadProfile

來自 Juan Cruz Fiant 的相應部落格文章:https ://dev.to/juxant/auto-refresh-with-dotnet-watch-for-asp-net-core-projects-20no

引用自:https://stackoverflow.com/questions/40152854