Ily*_*dik 20 dnx kestrel-http-server asp.net-core
我使用Visual Studio与ASP.NET Core并使用F5或Ctrl + F5(不直接使用命令行)运行网站.我想使用"dotnet watch"功能来确保动态获取所有更改以避免再次启动服务器.似乎使用命令行你会使用"dotnet watch run",但Visual Studio使用launchSettings.json并在幕后操作,如果我理解正确的话.
如何在那里连接"dotnet手表"?
Sha*_*tin 23
打开launchSettings.json并将其添加到profiles.
"Watch": {
"executablePath": "C:\\Program Files\\dotnet\\dotnet.exe",
"commandLineArgs": "watch run",
"launchBrowser": true,
"launchUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
Run Code Online (Sandbox Code Playgroud)
打开project.json并将其添加到tools.
"Microsoft.DotNet.Watcher.Tools": "1.0.0-preview2-final"
Run Code Online (Sandbox Code Playgroud)
恢复后,我们可以在Visual Studio中观看.
Дми*_*нов 17
只是对@Flynn 的回答稍加修正。你需要添加一个
"commandName": "Executable"
“观察”配置文件的参数。同样要定义网址,您不应在“观看”配置文件中定义它们,而应在配置文件中定义它们
"commandName": "Program"
参数(它存在于launchsettings.json由 Visual Studio 项目模板创建的 default 中,因此,您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"
}
Run Code Online (Sandbox Code Playgroud)
我将launchBrowser参数保留在Program配置文件中,但未启动浏览器。但是如果这个参数存在于Executable配置文件中,浏览器也不会启动,我发现无法自动启动它。
Fly*_*ynn 14
如果要使用ASP.NET 2.x或3.x,则需要对其进行一些更改。
语法略有不同
“观看”:{
“ executablePath”:“ dotnet.exe”,
“ workingDirectory”:“ $(ProjectDir)”,
“ commandLineArgs”:“监视运行”,
“ launchBrowser”:是的,
“ launchUrl”:“ http:// localhost:5000 /”,
“环境变量”: {
“ ASPNETCORE_ENVIRONMENT”:“发展”
}
}
更新:添加了“ workingDirectory”并删除了特定路径。现在更通用了。
小智 10
"Watch": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "http://localhost:5000/",
"commandLineArgs": "watch run",
"workingDirectory": "$(ProjectDir)",
"executablePath": "dotnet.exe",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
Run Code Online (Sandbox Code Playgroud)
这个也可以运行并启动浏览器。它工作是因为"commandName": "Project"线路,这意味着它将与 Kestrel 服务器一起启动。
接受的答案有效,但已经有 4 年以上的历史了。所以这里是如何使它适用于 Visual Studio 2019(在我的情况下为 v16.8.5)。
在 的profiles部分中launchSettings.json,您添加了一个新的配置文件,比如说“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"
}
Run Code Online (Sandbox Code Playgroud)
然后你去在构建配置文件下拉列表中选择它:
现在,当您运行它时,无论是否打开调试模式,重新构建和浏览器刷新(我使用 Swagger UI 作为默认页面)都会自动发生。
关于在调试模式下使用它的一个注意事项是,Visual Studio 会将更改标记为绿色,并表示在重新启动之前不会应用它们。我可以确认这不是真的,并且更改确实反映在dotnet watch run. 只是 VS 2019 感到困惑,并从旧(标准)的角度看待事物。
| 归档时间: |
|
| 查看次数: |
10610 次 |
| 最近记录: |