Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
542 views
in Technique[技术] by (71.8m points)

dnx - How to watch for file changes "dotnet watch" with Visual Studio ASP.NET Core

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For .Net 5 & 6 see last update!

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.

UPDATE (2021-05-19): For .Net 5 & 6

A) In VisualStudio 2019

  1. Go to Tools > ? Options > Projects and Solutions > ASP .NET Core
  2. Select Auto build and refresh browser after saving changes in Auto build and refresh option
  3. Press Ctrl + F5 (Start Without Debugging) IMPORTANT: Only works if run without debbuging

B) Otherwise add this to your launchSettings.json

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

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

  }
}

The automatically generated profile with "commandName":"Project" has all the other properties needed: launchBrowser, applicationUrl, environmentVariables, dotnetRunMessages and hotReloadProfile. Any modifications should be made there.

Corresponding Blog-Post from Juan Cruz Fiant: https://dev.to/juxant/auto-refresh-with-dotnet-watch-for-asp-net-core-projects-20no


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...