How to watch for file changes "dotnet watch" with Visual Studio ASP.NET Core
Asked Answered
J

8

73

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?

Josephson answered 20/10, 2016 at 11:14 Comment(2)
I think you have the wrong impression of "watch" functionality. When you change a file your application will be restarted and having to warm up again on first request or populate it's cache (as the in memory cached content get losts when it restarts)Staffard
Well, I really meant without having a need to "manually restart" the application. So I do understand that it's not some magic on the fly, which would be nice to have similar to the cshtml views recompilation that does not restart the whole application.Josephson
L
49

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

Lau answered 22/10, 2016 at 4:30 Comment(10)
Thanks for the answer. It was rather easy after all. Did not guess myself you can just directly use dotnet as from commandline with launchSettings.json :). Though it seems that it does full application restart, which can pretty much be achieved by running behind IIS Express and just building the application. If you do many changes I guess watch will restart on each file save. So the choice should probably depend on what stage of development you are (how often you change stuff).Josephson
Doesn't seem to pick up any changes. Anything else fancy for it to watch the appropriate directory?Montserrat
@Montserrat What is your directory structure? Where is your launch.json file? In other words, what have you tried? It might be worth opening a new question to specify the situation are you are facing.Lau
When running, it won't let me change any files from the IDE lol.Otti
@MatthewJamesDavis What do you mean, exactly? I'm curious.Lau
launchSettings are Visual Studio specific. In Visual Studio, I made this change and clicked "Start", which starts debugging. Visual Studio doesn't let you modify C# code while debugging. I can go in VS Code or another editor and make changes, but Visual Studio locks up. I ended up setting up my project exclusively in VS Code because the dev experience in Visual Studio was so sub par.Otti
@MatthewJamesDavis I am 100% VSCode these days for a variety of reasons. The chief one is the frequency with which full Visual Studio crashes.Lau
hi @ShaunLuttin I have tried watcher 2.0.0 with .net core 2.0.3 and it is not working, when I debug the application it will open blank hidden console and application will not launch, have you face any thing like thatJaniculum
For a 2021 answer with VS 2019 and .Net 5, you can check my answer below: https://mcmap.net/q/272324/-how-to-watch-for-file-changes-quot-dotnet-watch-quot-with-visual-studio-asp-net-coreCoxcomb
you forgot to add "commandName": "Watch", in the launchsettings watch node...Harlequin
C
60

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

For .Net 8 (with https and app.useHsts)

TLDR;

  1. Move the https profile to the top

  2. Add "ASPNETCORE_HTTPS_PORT": "7186" to the environment variables section for the https profile

  3. Add

    "Watch": {
      "commandName": "Executable",
      "executablePath": "dotnet.exe",
      "workingDirectory": "$(ProjectDir)",
      "commandLineArgs": "watch --non-interactive run",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "ASPNETCORE_HTTPS_PORT": "7186"
      },
      "dotnetRunMessages": true,
      "applicationUrl": "https://localhost:7186;http://localhost:5274"
    },
    

Things have changed. Visual Studio puts many different Profiles into your launchsettings.json, especially when using https and app.UseHsts.

First I had to move the https profile on top, as the first profile! Under "environmentVariables" I needed to add "ASPNETCORE_HTTPS_PORT": "7186" to make it work with lauchbrowser and the HTTPS redirect.

I added --non-interactive to force watch to restart the app if it can't hot reload and stops asking me.

My complete launchsettings.json

{
  "profiles": {
    "https": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "ASPNETCORE_HTTPS_PORT": "7186"
      },
      "dotnetRunMessages": true,
      "applicationUrl": "https://localhost:7186;http://localhost:5274"
    },
    "http": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
      },
      "dotnetRunMessages": true,
      "applicationUrl": "http://localhost:5274"
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
      }
    },
    "Watch": {
      "commandName": "Executable",
      "executablePath": "dotnet.exe",
      "workingDirectory": "$(ProjectDir)",
      "commandLineArgs": "watch --non-interactive run",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "ASPNETCORE_HTTPS_PORT": "7186"
      },
      "dotnetRunMessages": true,
      "applicationUrl": "https://localhost:7186;http://localhost:5274"
    },
    "WSL": {
      "commandName": "WSL2",
      "launchBrowser": true,
      "launchUrl": "https://localhost:7186",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "ASPNETCORE_URLS": "https://localhost:7186;http://localhost:5274",
        "ASPNETCORE_HTTPS_PORT": "7186"
      },
      "distributionName": ""
    }
  },
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:60552",
      "sslPort": 44360
    }
  }

If you start watch from the command line, it seems to use the default profile -> the first profile in the list.

If you want to explicitly start the Watch profile from the command-line, use this syntax:

dotnet watch --non-interactive --launch-profile "Watch" --project INSERT_PATH_TO_PROJECT
Cattegat answered 9/11, 2018 at 11:39 Comment(9)
This locked my c# files, while running, I cannot edit files, any clues?Peerless
@Peerless I am using this all the time. Without any problems. I have 2.2 Preview3 installed. What extactly do you mean by "can not edit files"?Cattegat
I think i was running in debug mode, which locks the files, there is another option to start without debugging, right? I forgot its been a long time since I worked with itPeerless
Probably you have Edit and Continue disabled? learn.microsoft.com/en-us/visualstudio/debugger/…Calore
This worked however I had to change the relative project path to go up 3 directories instead of 4. Eg: "commandLineArgs": "watch --project ..\\..\\..\\YOUR_PROJECT.csproj run"Alanealanine
If you add "workingDirectory": "$(ProjectDir)", you can remove the --project ... arg.Wollastonite
Despite "launchBrowser": true, my browser doesn't launch. Does this work for anyone else?Wollastonite
It won't start the browser for me too. I have to do it manually.Cattegat
@Peerless you need to hit CTRL+F5 not to click "Watch" or F5Electra
L
49

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

Lau answered 22/10, 2016 at 4:30 Comment(10)
Thanks for the answer. It was rather easy after all. Did not guess myself you can just directly use dotnet as from commandline with launchSettings.json :). Though it seems that it does full application restart, which can pretty much be achieved by running behind IIS Express and just building the application. If you do many changes I guess watch will restart on each file save. So the choice should probably depend on what stage of development you are (how often you change stuff).Josephson
Doesn't seem to pick up any changes. Anything else fancy for it to watch the appropriate directory?Montserrat
@Montserrat What is your directory structure? Where is your launch.json file? In other words, what have you tried? It might be worth opening a new question to specify the situation are you are facing.Lau
When running, it won't let me change any files from the IDE lol.Otti
@MatthewJamesDavis What do you mean, exactly? I'm curious.Lau
launchSettings are Visual Studio specific. In Visual Studio, I made this change and clicked "Start", which starts debugging. Visual Studio doesn't let you modify C# code while debugging. I can go in VS Code or another editor and make changes, but Visual Studio locks up. I ended up setting up my project exclusively in VS Code because the dev experience in Visual Studio was so sub par.Otti
@MatthewJamesDavis I am 100% VSCode these days for a variety of reasons. The chief one is the frequency with which full Visual Studio crashes.Lau
hi @ShaunLuttin I have tried watcher 2.0.0 with .net core 2.0.3 and it is not working, when I debug the application it will open blank hidden console and application will not launch, have you face any thing like thatJaniculum
For a 2021 answer with VS 2019 and .Net 5, you can check my answer below: https://mcmap.net/q/272324/-how-to-watch-for-file-changes-quot-dotnet-watch-quot-with-visual-studio-asp-net-coreCoxcomb
you forgot to add "commandName": "Watch", in the launchsettings watch node...Harlequin
F
18

Just one little correction to @Flynn`s answer. You need to add an

"commandName": "Executable"

argument to the "Watch" profile. Also to define the urls you should define them not in the "Watch" profile, but in the profile with

"commandName": "Program"

argument (it is present in the default launchsettings.json, created by the Visual Studio project templates, so, your launchsettings.json finally looks like this:

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

I kept the launchBrowser argument in the Program profile, but browser in not launched. But if this argument is present in the Executable profile, the browser is not launched too and I found no way to launch it automatically.

Fere answered 9/4, 2020 at 9:7 Comment(1)
@Harlequin "Project"Cubicle
C
17

The accepted answer works, but it's 4+ years old. So here's how you make it work for Visual Studio 2019 (v16.8.5 in my case).

Inside the profiles section of launchSettings.json, you add a new profile, let's say "API Watch", with this content:

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

And then you go and select it in the Build profiles dropdown:

enter image description here


Now when you run it, regardless if with or without Debug mode on, the re-build and browser refresh (I use the Swagger UI as default page) happens automatically.


One note about using it in Debug mode, is that Visual Studio will mark the changes with green and will say that they won't be applied until a restart happens. I can confirm that this is not true and that the changes are really reflected by the auto rebuild feature of dotnet watch run. It's just that VS 2019 gets confused and treats things from the old (standard) perspective.

enter image description here

Coxcomb answered 13/2, 2021 at 15:10 Comment(5)
Technically the best solution. Thank you.Paleoclimatology
After many comments and a couple of websites regarding this topic, your answer helped me. Thank you, Mike!Veil
This is the best solution, thanksChalice
@Mihai Paraschivescu ` "dotnetRunMessages": "true" or "dotnetRunMessages": true. I mean witth true in parenthesis or without?Houlberg
@Houlberg without parenthesis, as per the latest docs. I wasn't aware of this since there were no docs for it when I wrote my answer, but now they've clarified it. I will edit my answer to reflect it, thanks.Coxcomb
J
11
"Watch": {
  "commandName": "Project",
  "launchBrowser": true,
  "launchUrl": "http://localhost:5000/",
  "commandLineArgs": "watch run",
  "workingDirectory": "$(ProjectDir)",
  "executablePath": "dotnet.exe",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
}

This one will work and launch the browser too. It works because of the "commandName": "Project" line, which means it will be launching with the Kestrel server.

Joannejoannes answered 8/5, 2020 at 1:9 Comment(2)
This should be the accepted answer. VS 2019 v16.10.2.Cubicle
launchSettings.json is located in the Properties folderLysol
S
3

Open launchSettings.json and add this to profiles.

 "Watch": {
      "executablePath": "dotnet.exe",
      "commandLineArgs": "watch --project ..\\..\\..\\YourProject.csproj run",
      "launchBrowser": true,
      "launchUrl": "http://localhost:5000/",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
Supersensible answered 25/4, 2020 at 12:57 Comment(0)
M
3

To anyone else reading these really old answers and wondering if it is baked-in yet, then you should read this blog post from Nov 22, 2020.

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

Visual Studio 2019 now has a setting for ASP.NET Core to refresh when using IIS Express. By default it is not enabled.

You can still use the launchSettings.json files as described in the article.

Mccorkle answered 12/3, 2021 at 1:7 Comment(0)
B
1

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

here an image for confg

enter image description here

here an image for working project 2021-01-11

enter image description here

B answered 11/1, 2021 at 11:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.