Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 website using just F5 or Ctrl+F5 (not using the 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 the 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?

like image 826
Ilya Chernomordik Avatar asked Oct 20 '16 11:10

Ilya Chernomordik


People also ask

How do I run a dotnet watch?

Run .Run dotnet watch run in the WebApp folder. The console output indicates watch has started. Running dotnet watch run on a web app launches a browser that navigates to the app's URL once ready. dotnet watch does this by reading the app's console output and waiting for the ready message displayed by WebHost.

How do I view .NET core in Visual Studio?

NET Core is installed on Windows is: Press Windows + R. Type cmd. On the command prompt, type dotnet --version.

Can I use .NET core and .NET framework together?

At this point, if your NuGet dependencies are compatible with both your netcore and netframework targets you may be done! The big news here is that most of your NuGet dependencies are compatible with both. All of the most downloaded NuGet packages are either multi-targeted, or have packages for each target.

How do I Debug a .NET core code in Visual Studio?

Open the Debug view by selecting the Debugging icon on the left side menu. Select the green arrow at the top of the pane, next to . NET Core Launch (console). Other ways to start the program in debugging mode are by pressing F5 or choosing Run > Start Debugging from the menu.


2 Answers

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"   } } 

For .Net 5 & 6

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

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

like image 196
Michael A. Volz aka Flynn Avatar answered Sep 28 '22 08:09

Michael A. Volz aka Flynn


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.

enter image description here

like image 22
Shaun Luttin Avatar answered Sep 28 '22 06:09

Shaun Luttin