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:
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 watchwill 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).
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:
- Open the Run and Debug panel (
Ctrl + Shift + D). - Click "create a launch.json file" and select the ".NET Core" template.
- 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
.csprojfile):{ "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 } } ] }
- Open the command panel (
- Go back to your
launch.jsonfile and add apreLaunchTaskproperty 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.
- Razor views not updating? Make sure your project is running in the
Developmentenvironment. Check yourProperties/launchSettings.jsonfile—you should see this line under your profile:
Production environment caches views by default, so live reload won't work there."environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } - Static files stuck in browser cache? Press
Ctrl + F5to do a hard refresh, or enable "Disable cache" in your browser's dev tools (F12 > Network tab). dotnet watchnot 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.
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

