我正在尝试使用 dotnet run 命令在浏览器中启动 swagger UI。这是我在 launch.json 文件中尝试过的。预期结果是它应该在指定的 url 启动浏览器并自动打开 swagger UI。仅供参考,我正在使用 dotnet5 并创建了新的 web api 项目,该项目已经大摇大摆地融入其中。它不会启动浏览器。
如果您使用的是 dotnet run 命令,那么它的一个已知限制是它不会启动浏览器。
请参考此链接确认
虽然它没有启动它,但它仍然会让您的项目运行,这意味着您必须手动在浏览器中键入它或使用该问题中列出的解决方法之一。
使用化合物启动设置
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/src/WebApi/bin/Debug/net7.0/YourWebDll.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"console": "internalConsole"
},
{
"name": "Launch Chrome",
"request": "launch",
"type": "chrome",
"url": "https://localhost:5001/swagger/index.html",
"webRoot": "${workspaceFolder}"
}
],
"compounds": [
{
"name": ".Net Core & Chrome",
"configurations": [".NET Core Launch (console)","Launch Chrome"]
}
]
这将运行您的应用程序并在您想要的 URL 启动 chrome。
"uriFormat": "%s/swagger/index.html"
在你的 launcg.json 中添加上面的代码。
例子:
`
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/{your own project root}",
"args": [],
"cwd": "${workspaceFolder}/{your own project root}",
"stopAtEntry": false,
"serverReadyAction": {
"action": "openExternally",
"pattern": "\\bNow listening on:\\s+(https?://\\S+)",
"uriFormat": "%s/swagger/index.html"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
`
从 github 得到这个答案:https://github.com/dotnet/sdk/issues/9038#issuecomment-1380871802