How to watch for file changes “dotnet watch” with Visual Studio ASP.NET Core

断了今生、忘了曾经 提交于 2019-12-29 04:33:12

问题


I am using Visual Studio with ASP.NET Core and run the web site using just F5 or Ctrl+F5 (not using command line directly). I would like to use the "dotnet watch" functionality to make sure all changes are picked up on the fly to avoid starting the server again. It seems that with command line you would use "dotnet watch run" for this, but Visual Studio uses launchSettings.json and does it behind the scenes if I understand it correctly.

How can I wire up "dotnet watch" there?


回答1:


Open launchSettings.json and add this to profiles.

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

Open project.json and add this to tools.

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

After restoring, we can Watch from within Visual Studio.




回答2:


If you want to use ASP.NET 2.x or 3.x you need to change it a bit.

  • The watch tool is a global tool now and you don't need to add it as a reference any longer
  • The syntax is slightly different

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

UPDATE: added "workingDirectory" and removed specific path. It's more generic now.



来源:https://stackoverflow.com/questions/40152854/how-to-watch-for-file-changes-dotnet-watch-with-visual-studio-asp-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!