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

前端 未结 5 1051
花落未央
花落未央 2020-12-05 02:06

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\" function

相关标签:
5条回答
  • 2020-12-05 02:26

    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.

    0 讨论(0)
  • 2020-12-05 02:36

    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.

    0 讨论(0)
  • 2020-12-05 02:37

    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.

    0 讨论(0)
  • 2020-12-05 02:43

    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"
          }
        },
    
    0 讨论(0)
  • 2020-12-05 02:48
    "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.

    0 讨论(0)
提交回复
热议问题