You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VS Code中实现ASP.NET Core Web应用的调试热重载

实现.NET项目一键调试+自动刷新+保持调试模式的配置方案

需求目标

  • 点击VS Code调试按钮后,自动启动项目并打开浏览器
  • 修改代码后自动刷新页面,同步变更内容
  • 全程保持调试模式,无需手动重新附加进程

当前问题

手动执行dotnet watch后再通过调试附加进程,操作繁琐,且无法自动触发浏览器打开,未实现完全自动化的调试流程。

配置方案

1. 配置任务文件 .vscode/tasks.json

创建或修改该文件,添加自动运行dotnet watch的任务:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "dotnet watch",
            "command": "dotnet",
            "args": [
                "watch",
                "run",
                "--no-hot-reload"
            ],
            "type": "process",
            "problemMatcher": "$msCompile",
            "isBackground": true,
            "presentation": {
                "reveal": "never"
            }
        }
    ]
}

2. 修改调试配置 .vscode/launch.json

更新配置,实现自动启动watch任务、附加进程并打开浏览器:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Watch + Debug",
            "type": "coreclr",
            "request": "attach",
            "processId": "${command:pickProcess}",
            "preLaunchTask": "dotnet watch",
            "postDebugTask": "stop-dotnet-watch",
            "serverReadyAction": {
                "action": "openExternally",
                "pattern": "\\bNow listening on:\\s+(https?://\\S+)"
            }
        }
    ],
    "tasks": [
        {
            "label": "stop-dotnet-watch",
            "command": "pkill",
            "args": ["dotnet"],
            "type": "shell",
            "problemMatcher": []
        }
    ]
}

操作说明

  1. 保存上述两个配置文件
  2. 在VS Code的调试面板中选择.NET Watch + Debug配置
  3. 点击调试启动按钮:
    • 自动执行dotnet watch run启动项目
    • 弹出进程选择框,选择对应的dotnet进程完成附加
    • 项目启动后自动打开浏览器访问地址
    • 修改代码后,dotnet watch会自动重建项目,浏览器页面刷新,调试模式保持有效

注意事项

  • 若使用Windows系统,stop-dotnet-watch任务的command需改为taskkill,args改为["/F", "/IM", "dotnet.exe"]
  • 确保已安装.NET SDK及VS Code的C#扩展

内容的提问来源于stack exchange,提问作者arnie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 01:56:03