如何在Visual Studio ASP.NET Core中使用"dotnet watch"观察文件变化

72
我正在使用Visual Studio和ASP.NET Core,并通过F5或Ctrl+F5(而非直接使用命令行)运行网站。我想使用“dotnet watch”功能来确保及时检测到所有更改,以避免重新启动服务器。似乎在命令行中,您需要使用“dotnet watch run”,但是如果我理解正确,Visual Studio使用launchSettings.json并在幕后完成此操作。

如何连接“dotnet watch”?


我认为你对“watch”功能有误解。当你更改文件时,你的应用程序将会重新启动,并且需要在第一次请求时再次进行预热或填充缓存(因为内存中缓存的内容在重新启动后会丢失)。 - Tseng
好的,我的意思是不需要“手动重启”应用程序。所以我确实理解这不是一些即时的魔法,如果能像cshtml视图重新编译一样不需要重启整个应用程序那就太好了。 - Ilya Chernomordik
8个回答

61
如果你想使用ASP.NET 2.x或3.x,你需要稍微修改一下。
  • 现在,watch工具是一个全局工具,您不再需要将其添加为引用

  • 语法稍有不同

    "Watch": {
      "executablePath": "dotnet.exe",
      "workingDirectory": "$(ProjectDir)",
      "commandLineArgs": "watch run",
      "launchBrowser": true,
      "launchUrl": "http://localhost:5000/",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
    

对于 .Net 5 和 6

在 VisualStudio 2019 中

  1. 转到工具 > ⚙ 选项 > 项目和解决方案 > ASP .NET Core
  2. 选择自动构建并在保存更改后刷新浏览器
  3. 按下 Ctrl + F5(无调试启动)重要提示:仅在无调试运行时有效

否则,请将以下内容添加到您的 launchSettings.json 文件中:

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

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

  }
}

自动生成的具有“commandName”:“Project”的profile已经具备了所有其他所需的属性:launchBrowserapplicationUrlenvironmentVariablesdotnetRunMessageshotReloadProfile。任何修改都应该在那里进行。
对应的Juan Cruz Fiant的博客文章:https://dev.to/juxant/auto-refresh-with-dotnet-watch-for-asp-net-core-projects-20no

对于.Net 8(使用httpsapp.useHsts

简而言之;

  1. https配置文件移到顶部

  2. 在环境变量部分为https配置文件添加"ASPNETCORE_HTTPS_PORT": "7186"

  3. 添加

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

事情变了。在使用https和app.UseHsts时,Visual Studio将许多不同的配置文件放入你的launchsettings.json中。
首先,我必须将https配置文件移到顶部,作为第一个配置文件!在"environmentVariables"下面,我需要添加"ASPNETCORE_HTTPS_PORT": "7186",以使其与lauchbrowser和HTTPS重定向一起正常工作。
我添加了--non-interactive来强制watch在无法热重载并停止询问我的情况下重新启动应用程序。
我的完整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
    }
  }

如果你从命令行开始观看,它似乎使用默认配置文件 -> 列表中的第一个配置文件。
如果你想要明确从命令行开始 Watch 配置文件,请使用以下语法:
dotnet watch --non-interactive --launch-profile "Watch" --project INSERT_PATH_TO_PROJECT

2
我的C#文件被锁定了,运行时我无法编辑文件,有什么线索吗? - Ayyash
2
这个方法可行,但是我必须将相对项目路径改为向上3个目录而不是4个。例如: "commandLineArgs": "watch --project ..\\..\\..\\YOUR_PROJECT.csproj run" - burnt1ce
2
如果您添加了 "workingDirectory": "$(ProjectDir)",,则可以删除 --project ... 参数。 - DharmaTurtle
4
尽管设置了"launchBrowser": true,我的浏览器没有启动。这对其他人是否有效? - DharmaTurtle
4
我也无法启动浏览器。我必须手动启动它。 - Michael A. Volz aka Flynn
显示剩余4条评论

50

打开 launchSettings.json 文件并将以下内容添加到 profiles 中。

  "Watch": {
    "executablePath": "C:\\Program Files\\dotnet\\dotnet.exe",
    "commandLineArgs": "watch run",
    "launchBrowser": true,
    "launchUrl": "http://localhost:5000",
    "environmentVariables": {
      "ASPNETCORE_ENVIRONMENT": "Development"
    }
  }

打开 project.json 文件并将以下代码添加到 tools 中。

"Microsoft.DotNet.Watcher.Tools": "1.0.0-preview2-final"

恢复后,我们可以在 Visual Studio 中观看。

输入图像描述


1
谢谢你的回答。毕竟这很容易。我没有想到你可以直接使用launchSettings.json从命令行中使用dotnet :)。虽然它似乎会进行完整的应用程序重启,但这基本上可以通过在IIS Express后台运行并构建应用程序来实现。如果您进行了许多更改,我猜watch将在每次文件保存时重新启动。因此,选择可能取决于您开发的阶段(您更改内容的频率)。 - Ilya Chernomordik
似乎没有检测到任何更改。还有其他高级功能可以监视适当的目录吗? - Marchy
5
运行时,它不会让我从IDE修改任何文件。笑。 - Matthew James Davis
launchSettings 是 Visual Studio 特有的。在 Visual Studio 中,我进行了更改并单击“开始”,以启动调试。Visual Studio 不允许在调试时修改 C#代码。我可以进入 VS Code 或另一个编辑器进行更改,但是 Visual Studio 会锁定。最终我决定完全在 VS Code 中设置我的项目,因为 Visual Studio 的开发体验太差了。 - Matthew James Davis
3
你好@ShaunLuttin,我已经尝试在.NET Core 2.0.3中使用Watcher 2.0.0,但是它无法运行。当我调试应用程序时,它会打开一个空白的隐藏控制台,并且应用程序无法启动。你有遇到过类似的情况吗? - programtreasures
显示剩余5条评论

18

对于 @Flynn 的回答,只需要做一个小修改。你需要添加一个

"commandName": "Executable"

为“Watch”配置文件定义参数。另外,要定义URL,您不应该在“Watch”配置文件中定义,而应在

"commandName": "Program"

参数(它在默认的 launchsettings.json 中存在,由 Visual Studio 项目模板创建,所以你的 launchsettings.json 最后看起来像这样:

)。

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

我保留了Program配置文件中的launchBrowser参数,但浏览器并没有启动。但如果该参数存在于Executable配置文件中,则浏览器也不会启动,我找不到自动启动它的方法。


@serge "项目" - Meeting Attender

16

被接受的答案是有效的,但已有4年以上的时间了。所以这里是如何使其适用于Visual Studio 2019(在我的情况下为v16.8.5)。

launchSettings.jsonprofiles部分内,您可以添加一个新的配置文件,比如说“API Watch”,并加入以下内容:

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

然后你去在“构建配置文件”下拉菜单中选择它:

enter image description here


现在当您运行它时,无论是否开启调试模式,重新构建和浏览器刷新(我使用Swagger UI作为默认页面)都会自动发生。
关于在调试模式下使用它的一个注意事项是,Visual Studio会用绿色标记更改,并表示在重新启动之前不会应用这些更改。我可以确认这不是真的,而且这些更改确实通过dotnet watch run的自动重建功能得到了反映。只是VS 2019会混淆并将事物视为旧(标准)的角度。

enter image description here


技术上最佳方案。谢谢。 - Paul-Sebastian Manole
在许多评论和几个网站关于这个话题之后,你的回答帮助了我。谢谢你,Mike! - Moises Gonzaga
这是最好的解决方案,谢谢。 - Omer
@Mihai Paraschivescu "dotnetRunMessages": "true" 或者 "dotnetRunMessages": true。我的意思是带括号的true`还是不带? - luca88
1
根据最新文档,使用@luca88(不带括号)。当我回答时还没有相关文档,所以我不知道这一点,但现在他们已经澄清了。我会编辑我的答案以反映这一点,谢谢。 - Mihai Paraschivescu

11
"Watch": {
  "commandName": "Project",
  "launchBrowser": true,
  "launchUrl": "http://localhost:5000/",
  "commandLineArgs": "watch run",
  "workingDirectory": "$(ProjectDir)",
  "executablePath": "dotnet.exe",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
}

这个可以工作并且还能启动浏览器。它之所以工作,是因为有一行"commandName": "Project",这意味着它将与Kestrel服务器一起启动。


1
这应该是被接受的答案。VS 2019 v16.10.2。 - Meeting Attender
launchSettings.json位于Properties文件夹中。 - gl3yn

3

打开launchSettings.json,并将以下内容添加到配置文件中。

 "Watch": {
      "executablePath": "dotnet.exe",
      "commandLineArgs": "watch --project ..\\..\\..\\YourProject.csproj run",
      "launchBrowser": true,
      "launchUrl": "http://localhost:5000/",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },

3

如果有其他人阅读这些非常老的答案,并想知道它是否已经内置,那么您应该阅读这篇来自2020年11月22日的博客文章。

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

Visual Studio 2019现在有一个设置用于ASP.NET Core在使用IIS Express时进行刷新。默认情况下,它未启用。

您仍然可以像文章中描述的那样使用launchSettings.json文件。


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

这是一个关于配置的图片。

enter image description here

这是一个2021年01月11日的工作项目图片。

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接