如何连接“dotnet watch”?
如何连接“dotnet watch”?
现在,watch工具是一个全局工具,您不再需要将其添加为引用
语法稍有不同
"Watch": {
"executablePath": "dotnet.exe",
"workingDirectory": "$(ProjectDir)",
"commandLineArgs": "watch run",
"launchBrowser": true,
"launchUrl": "http://localhost:5000/",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
在 VisualStudio 2019 中
否则,请将以下内容添加到您的 launchSettings.json
文件中:
{
"iisSettings": {
...
},
"profiles": {
... ,
"Watch": {
"commandName": "Executable",
"executablePath": "dotnet.exe",
"workingDirectory": "$(ProjectDir)",
"commandLineArgs": "watch run"
}
}
}
profile
已经具备了所有其他所需的属性:launchBrowser
,applicationUrl
,environmentVariables
,dotnetRunMessages
和hotReloadProfile
。任何修改都应该在那里进行。https
和app.useHsts
)将https
配置文件移到顶部
在环境变量部分为https
配置文件添加"ASPNETCORE_HTTPS_PORT": "7186"
添加
"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"
},
{
"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
"commandLineArgs": "watch --project ..\\..\\..\\YOUR_PROJECT.csproj run"
- burnt1ce"workingDirectory": "$(ProjectDir)",
,则可以删除 --project ...
参数。 - DharmaTurtle"launchBrowser": true
,我的浏览器没有启动。这对其他人是否有效? - DharmaTurtle打开 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 中观看。
对于 @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
配置文件中,则浏览器也不会启动,我找不到自动启动它的方法。
被接受的答案是有效的,但已有4年以上的时间了。所以这里是如何使其适用于Visual Studio 2019(在我的情况下为v16.8.5)。
在launchSettings.json
的profiles
部分内,您可以添加一个新的配置文件,比如说“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
}
然后你去在“构建配置文件”下拉菜单中选择它:
dotnet watch run
的自动重建功能得到了反映。只是VS 2019会混淆并将事物视为旧(标准)的角度。
"dotnetRunMessages": "true"
或者 "dotnetRunMessages": true。我的意思是带括号的
true`还是不带? - luca88"Watch": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "http://localhost:5000/",
"commandLineArgs": "watch run",
"workingDirectory": "$(ProjectDir)",
"executablePath": "dotnet.exe",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
这个可以工作并且还能启动浏览器。它之所以工作,是因为有一行"commandName": "Project"
,这意味着它将与Kestrel服务器一起启动。
打开launchSettings.json,并将以下内容添加到配置文件中。
"Watch": {
"executablePath": "dotnet.exe",
"commandLineArgs": "watch --project ..\\..\\..\\YourProject.csproj run",
"launchBrowser": true,
"launchUrl": "http://localhost:5000/",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
如果有其他人阅读这些非常老的答案,并想知道它是否已经内置,那么您应该阅读这篇来自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文件。
{
"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"
}
}
}