54
votes

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?

8
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) - Tseng
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. - Ilya Chernomordik

8 Answers

47
votes

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

49
votes

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

17
votes

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.

10
votes
"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.

5
votes

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

2
votes

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"
      }
    },
2
votes

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.

1
votes

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