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

VS Code中实现ASP.NET代码实时刷新:无需重复运行dotnet应用

Hey there! Getting real-time refresh for your ASP.NET Core apps in VS Code is totally doable—no more restarting dotnet run every time you tweak code. Let's break down how to set this up properly:

1. Use the Official dotnet watch Tool (Core of Real-Time Refresh)

This is the official, supported way to get live updates for your ASP.NET Core project. Here's how to use it:

  • Open VS Code's integrated terminal (press Ctrl + `` to toggle it) and navigate to your project's root folder (where the .csproj` file lives).
  • Run the command: dotnet watch run
  • This will start your application and immediately begin watching for file changes. Here's what happens when you modify and save different types of files:
    • Static files (CSS, JS, HTML): Just refresh your browser—changes will show up instantly, no app restart needed.
    • Razor views/pages: Save the file, then refresh the browser. ASP.NET Core's development environment automatically reloads views without restarting the app (more on environment setup below).
    • Backend code (Controllers, Models, Services): dotnet watch will detect the change, automatically recompile your project, and restart the app. Your browser will even refresh automatically (if you're using .NET 6+—older versions might need a manual refresh).
2. Configure VS Code to Use dotnet watch by Default

If you don't want to type dotnet watch run every time you start working, you can set up VS Code to launch with watch mode automatically:

  1. Open the Run and Debug panel (Ctrl + Shift + D).
  2. Click "create a launch.json file" and select the ".NET Core" template.
  3. Next, set up a task for dotnet watch:
    • Open the command panel (Ctrl + Shift + P), search for "Tasks: Configure Task", and select ".NET Core" from the options.
    • Replace the default task with this configuration (update the project path to match your .csproj file):
      {
        "version": "2.0.0",
        "tasks": [
          {
            "label": "watch",
            "command": "dotnet",
            "type": "process",
            "args": [
              "watch",
              "run",
              "--project",
              "${workspaceFolder}/YourProjectName.csproj"
            ],
            "problemMatcher": "$msCompile",
            "isBackground": true,
            "group": {
              "kind": "build",
              "isDefault": true
            }
          }
        ]
      }
      
  4. Go back to your launch.json file and add a preLaunchTask property pointing to the "watch" task:
    {
      "version": "0.2.0",
      "configurations": [
        {
          "name": ".NET Core Launch (web)",
          "type": "coreclr",
          "request": "launch",
          "preLaunchTask": "watch",
          "program": "${workspaceFolder}/bin/Debug/net6.0/YourProjectName.dll",
          "args": [],
          "cwd": "${workspaceFolder}",
          "stopAtEntry": false,
          "serverReadyAction": {
            "action": "openExternally",
            "pattern": "\\bNow listening on:\\s+(https?://\\S+)"
          },
          "env": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          },
          "sourceFileMap": {
            "/Views": "${workspaceFolder}/Views"
          }
        }
      ]
    }
    

Now when you hit F5 to start debugging, VS Code will automatically launch dotnet watch run for you.

3. Fix Common Refresh Issues
  • Razor views not updating? Make sure your project is running in the Development environment. Check your Properties/launchSettings.json file—you should see this line under your profile:
    "environmentVariables": {
      "ASPNETCORE_ENVIRONMENT": "Development"
    }
    
    Production environment caches views by default, so live reload won't work there.
  • Static files stuck in browser cache? Press Ctrl + F5 to do a hard refresh, or enable "Disable cache" in your browser's dev tools (F12 > Network tab).
  • dotnet watch not detecting changes? Double-check that you're saving your files (Ctrl + S). You can also enable VS Code's Auto Save feature (File > Auto Save) to avoid forgetting.
4. Bonus: Auto-Refresh Browser Without Manual Reload

If you want to skip even the manual refresh step, .NET 6 and later include built-in browser auto-refresh support with dotnet watch run. When you start the app with this command, it injects a small script into your pages that automatically refreshes the browser whenever files change. No extra plugins needed!


内容的提问来源于stack exchange,提问作者Alex-fergi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 10:32:48