1.背景
在Cursor下调试.Net Core WebAPI程序会出现下面的问题:
因为Cursor是VS Code的变种版本,并不被官方的调试机制支持去使用。基于这种情况,就产生了本文。
2.解决方法
使用三星电子的开源调试工具netcoredbg,就能解决这个问题
2.1 下载NetCoreDbg debugger
下载地址:https://github.com/Samsung/netcoredbg/releases
一般选择最新版本就行。我这里选的是 “netcoredbg-win64.zip”
2.2 解压netcoredbg
将步骤2.1下载的压缩包netcoredbg-win64.zip解压到C:\netcoredbg
完整地址是C:\netcoredbg\netcoredbg.exe
2.3.打开Cursor
然后在项目根目录下创建.vscode
文件夹,在文件夹下创建俩个文件launch.json、tasks.json
launch.json:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "debugTest01",//这是调试配置的名称,在 Visual Studio Code 中将显示此名称。
"type": "coreclr",//指定调试器类型为 .NET Core。
"request": "launch",//指定启动调试会话的方式。
"preLaunchTask": "build",
"program": "${workspaceFolder}/WebAPIVSCode/bin/Debug/net8.0/WebAPIVSCode.dll",//指定要调试的应用程序的路径。请确保路径和文件名与实际的应用程序输出目录和文件名匹配。
"cwd": "${workspaceFolder}",//指定工作目录为项目文件夹[Content root path: D:\ASP.NETCoreTest]
"requireExactSource": false,//注意十分的重要,是否要求精确匹配源代码。如果设置为 true,则要求精确匹配。如果设置为 false,则允许从其他位置加载源代码。
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"//在调试期间设置环境变量 ASPNETCORE_ENVIRONMENT 的值为 "Development"。这对于在开发环境中调试应用程序很有用。[Information: Hosting environment: Development]
},
"pipeTransport": {
"pipeCwd": "${workspaceFolder}",
"pipeProgram": "cmd",
"pipeArgs": ["/c"],
"debuggerPath": "C:\\netcoredbg\\netcoredbg.exe",
"debuggerArgs": ["--interpreter=vscode"],
"quoteArgs": true
},
}
]
}
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}"
],
"problemMatcher": "$msCompile"
}
]
}
2.4 调试运行
在代码设置断点。按F5启动
从上图可以。我们成功运行,调试。
3.结论
本文是一个记录过程,帮助大家如何更好用好Cursor去开发C# .Netcore WebAPI项目。为了方便大家参考,我将相关资料和程序打包,有需要可以自取。
链接: https://pan.baidu.com/s/1WYj4-IJSrT7KMAQvJ0G_9w?pwd=5a2y
提取码: 5a2y