簡體   English   中英

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

[英]How to watch for file changes "dotnet watch" with Visual Studio ASP.NET Core

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

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

如果您想使用 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"
    }

  }
}

使用"commandName":"Project"自動生成的profile具有所需的所有其他屬性: launchBrowserapplicationUrlenvironmentVariablesdotnetRunMessageshotReloadProfile 應該在那里進行任何修改。

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

打開 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 中觀看。

在此處輸入圖像描述

對@Flynn 的回答稍作修正。 您需要添加一個

"commandName": "Executable"

“Watch”配置文件的參數。 另外要定義網址,您應該不在“Watch”配置文件中定義它們,而是在配置文件中定義它們

"commandName": "Program"

參數(它存在於由 Visual Studio 項目模板創建的默認launchsettings.json中,因此,您的launchsettings.json最終看起來像這樣:

"AnyTest.WebClient": {
  "commandName": "Project",
  "launchBrowser": true,
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  },
  "launchUrl": "",
  "applicationUrl": "https://localhost:44353;http://localhost:51895",
  "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}"
},
"Watch": {
  "commandName": "Executable",
  "workingDirectory": "$(ProjectDir)",
  "executablePath": "dotnet.exe",
  "commandLineArgs": "watch run"
}

我在Program配置文件中保留了launchBrowser參數,但未啟動瀏覽器。 但是如果這個參數出現在Executable配置文件中,瀏覽器也不會啟動,我發現沒有辦法自動啟動它。

接受的答案有效,但它已有 4 年以上的歷史。 因此,這是使它適用於 Visual Studio 2019(在我的情況下為 v16.8.5)的方法。

launchSettings.jsonprofiles部分中,您添加了一個新配置文件,比如“API Watch”,其內容如下:

"API Watch": {
  "commandName": "Executable",
  "executablePath": "dotnet",
  "commandLineArgs": "watch run",
  "workingDirectory": "$(ProjectDir)",
  "launchBrowser": true,
  "applicationUrl": "https://localhost:5001;http://localhost:5000",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  },
  "dotnetRunMessages": "true"
}

然后你去構建配置文件下拉列表中選擇它:

在此處輸入圖像描述


現在,當您運行它時,無論是否打開調試模式,重新構建和瀏覽器刷新(我使用 Swagger UI 作為默認頁面)都會自動發生。


關於在調試模式下使用它的一個注意事項是,Visual Studio 會將更改標記為綠色,並說在重新啟動之前不會應用它們。 我可以確認這不是真的,並且更改確實反映在dotnet watch run的自動重建功能中。 只是 VS 2019 搞糊塗了,從舊的(標准)角度看待事物。

在此處輸入圖像描述

"Watch": {
  "commandName": "Project",
  "launchBrowser": true,
  "launchUrl": "http://localhost:5000/",
  "commandLineArgs": "watch run",
  "workingDirectory": "$(ProjectDir)",
  "executablePath": "dotnet.exe",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
}

這個也可以工作並啟動瀏覽器。 它的工作原理是"commandName": "Project"行,這意味着它將與 Kestrel 服務器一起啟動。

對於其他閱讀這些非常古老的答案並想知道它是否已被納入的人,那么您應該閱讀 2020 年 11 月 22 日的這篇博文。

https://dev.to/juxant/auto-refresh-with-dotnet-watch-for-asp-net-core-projects-20no

Visual Studio 2019 現在有一個設置,用於在使用 IIS Express 時刷新 ASP.NET Core。 默認情況下未啟用。

您仍然可以使用文章中所述的 launchSettings.json 文件。

打開 launchSettings.json 並將其添加到配置文件中。

 "Watch": {
      "executablePath": "dotnet.exe",
      "commandLineArgs": "watch --project ..\\..\\..\\YourProject.csproj run",
      "launchBrowser": true,
      "launchUrl": "http://localhost:5000/",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },

在 Visual Studio 2019 中

{
    "profiles": {
        "msteamsimc": {
        "commandName": "Executable",
        "executablePath": "dotnet",
        "commandLineArgs": "watch run",
        "workingDirectory": "$(ProjectDir)",
        "launchBrowser": true,
        "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        },
        "dotnetRunMessages": "true",
        "applicationUrl": "https://localhost:5001;http://localhost:5000"
    }
    }
}

這是配置的圖像

在此處輸入圖像描述

這是工作項目的圖像 2021-01-11

在此處輸入圖像描述

暫無
暫無

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

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