I have an ASP.NET Core 8.0 MVC project and I'm running into a really strange behavior with VS Code. I've been trying to update some routes and my views, and for some reason, it wasn't recognizing my change in my route. It kept on redirecting to one particular area, no matter what I changed. I tried to change everything I could, but it wouldn't correct it. I finally opened the project up in VS 2022 and the behavior went away and everything seemed normal.
So I deleted the area that it was redirecting to and when I started the project the area and views are all still present even after physically deleting the area folder that contained the views. I tried clearing various caches and I tried uninstalling and reinstalling VS Code but nothing has corrected it.
Is there a Windows cache that I'm missing? Maybe a temp file? How does VS Code do caching of the views?
This was my original launch profile:
"configurations": [
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/dotnet/vscode-csharp/blob/main/debugger-launchjson.md.
"name": "Dev .NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/net8.0/MyCoreProject.Web.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
"serverReadyAction": {
"action": "openExternally",
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
},
}
]
UPDATE
Tips: All the following modifications are only valid for Views files, not for Route or other C# codes.
Please try to use below setting in launch.json
, then we can enable hot-reload feature like vs2022.
Test Result
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "debug-hot-reload",
"type": "coreclr",
"request": "launch",
"program": "dotnet",
"args": [
"watch",
"--project",
".",
"--verbose"
],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"serverReadyAction": {
"action": "openExternally",
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development",
"Key": "Value"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}
This seems to be a known issue. As we know, in asp.net core projects, when .net core version <=5
, the compiled files of the page are stored in projectname.view.dll
. After version >=6
, the views files are stored in projectname.dll
.
Related doc: Razor: Compiler no longer produces a Views assembly.
This means that when making real-time modifications, you must compile again to update in real time.
So Microsoft officially gave the solution below.
1. Install the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
NuGet package.
2. Call AddRazorRuntimeCompilation in Program.cs:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages()
.AddRazorRuntimeCompilation();
// need tp add .AddRazorRuntimeCompilation()
// builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation();