【发布时间】:2021-06-01 16:24:17
【问题描述】:
我正在尝试在 Linux 上使用 .NET 5.0,使用 VS Code。
我创建了一个空目录,运行“code”。在 VS Code 中打开它,然后创建示例项目:
dotnet new webapi -n 实验
当“'实验'中缺少构建和调试所需的资产。添加它们?”出现对话框,我点击“是”。
我运行了构建任务,然后从“运行”菜单中选择了“开始调试”。
该项目似乎正在运行,它打开了一个 Chrome 浏览器选项卡到 https://localhost:5001/,该选项卡显示 HTTP ERROR 404。
lauchSettings.json 中的“Expementing”配置文件将 applicationUrl 设置为“https://localhost:5001;http://localhost:5000”,调试控制台包括:
现在监听:https://localhost:5001 现在收听:http://localhost:5000
所以端口似乎是正确的。
如果我尝试在 http://localhost:5000 上打开浏览器选项卡,它会重定向到 https://localhost:5001,然后显示 404。
我错过了什么?
jmoerdyk 的评论是正确的 - 如果我尝试 https://localhost:5001/swagger,我会得到 swagger 页面。
在 .NET Framework 中,Web API 在域根目录创建登录页面。 .NET Core 没有。
那么,问题是为什么浏览器选项卡会在 https://localhost:5001 而不是 https://localhost:5001/swagger 打开?
我在 launchSettings.json 中注意到了这一点:
"profiles": {
"IIS Express": {
...
},
"Experimenting": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
我认为应该打开指向招摇页面的浏览器。浏览一下,我看到了 VS Code 不使用 launchSettings.json 的声明,但很明显,因为如果我更改 applicationUrl 中的端口,我会在新端口上运行招摇页面。
但launchUrl似乎被忽略了。
浏览网页给了我同样的错误和过时的答案,这在 .NET Core 问题中很常见,但我最终尝试在 launch.json 中设置 uriFormat:
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (web)",
...
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
"serverReadyAction": {
"action": "openExternally",
"pattern": "\\bNow listening on:\\s+(https?://\\S+)",
"uriFormat": "%s/swagger"
},
...
},
]
}
评论中的链接解释了这应该如何工作 - 有点。
【问题讨论】:
-
正如预期的那样。在模板创建的代码中,根路径或默认路由上没有端点,因此
404 Not Found。当您访问https://localhost:5001/swagger/或https://localhost:5001/WeatherForecast时会发生什么?