Search code examples
asp.net-corevisual-studio-code.net-6.0

VSCode - Refresh Local Debugging HTML Changes Do Not Show if Subfolder


I have a NET Core solution folder that has 2 sub-folders in VS Code on a Mac.

MVCSolution (top folder)
+--- MVCApp (subfolder)
+--- MVCApp.Tests (subfolder)

MVCApp is just a vanilla MVC app, pretty much out of the box template. MVCApp.Tests are a few Unit Tests. All work just fine.

My .vscode and /Properties folders are located in the MVCSolution root.

The .vscode config json files all have references to the subfolder. For example, in launch.json:

...
"sourceFileMap": {
  "/Views": "${workspaceFolder}/MVCApp/Views"
}
...

Everything working hunky-dory with 1 exception. During a local debug session (Chrome browser), if I make a change to the HTML of one of my razor pages (eg. Home/Index change the h1 tag from 'Home' -> 'Home2'), and refresh the page in Chrome, it doesn't change. If I stop the debugging session and relaunch again, it will change then. It's kind of annoying, especially when I'm just tweaking client look&feel stuff.

I'm sure I've got a config wrong or missing some setting somewhere.

If I use VS Code and open just the MVCApp folder and add the .vscode configs and Properties back in to the MVCApp folder with correct paths, all works fine. Changes are reflected on browser refresh.

Any ideas of how to fix?

** ** EDIT ** **

For those looking for sample code, here's how you can replicate. Open up VSCode and go into the terminal. Start by making a solution directory and type in the following commands to create a sample solution with 2 projects.

mkdir TestMVC

dotnet new mvc -n TestMVCApp

dotnet new xunit -n TestMVCApp.Tests

Then, open the folder TestMVC in VS Code. You'll need to add the launch.json and tasks.json files to .vscode folder

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Launch (web)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "${workspaceFolder}/TestMVCApp/bin/Debug/net6.0/TestMVCApp.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            "stopAtEntry": false,
            "serverReadyAction": {
                "action": "openExternally",
                "pattern": "\\bNow listening on:\\s+(https?://\\S+)"
            },
            "env": {
                "ASPNETCORE_ENVIRONMENT": "Development"
            },
            "sourceFileMap": {
                "/Views": "${workspaceFolder}/TestMVCApp/Views"
            },
            "logging": {
                "moduleLoad": false,
                "engineLogging": false,
                "exceptions": true,
                "programOutput": true,
                "browserStdOut": false            
            }
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach"
        }
    ]
}

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "command": "dotnet",
            "type": "process",
            "args": [
                "build",
                "${workspaceFolder}/TestMVCApp/TestMVCApp.csproj",
                "/property:GenerateFullPaths=true",
                "/consoleloggerparameters:NoSummary"
            ],
            "problemMatcher": "$msCompile"
        },
        {
            "label": "publish",
            "command": "dotnet",
            "type": "process",
            "args": [
                "publish",
                "${workspaceFolder}/TestMVCApp.csproj",
                "/property:GenerateFullPaths=true",
                "/consoleloggerparameters:NoSummary"
            ],
            "problemMatcher": "$msCompile"
        },
        {
            "label": "watch",
            "command": "dotnet",
            "type": "process",
            "args": [
                "watch",
                "run",
                "${workspaceFolder}/TestMVCApp/TestMVCApp.csproj",
                "/property:GenerateFullPaths=true",
                "/consoleloggerparameters:NoSummary"
            ],
            "problemMatcher": "$msCompile"
        },
        {
            "label": "clean",
            "command": "dotnet",
            "type": "process",
            "args": [
                "clean",
                "${workspaceFolder}/TestMVCApp/",
                "/property:GenerateFullPaths=true",
                "/consoleloggerparameters:NoSummary"
            ],
            "problemMatcher": "$msCompile"
        },
        {
            "label": "publish-release",
            "command": "dotnet",
            "type": "process",
            "args": [
                "publish",
                "${workspaceFolder}/TestMVCApp/",
                "--configuration",
                "Release",
                "/property:GenerateFullPaths=true",
                "/consoleloggerparameters:NoSummary"
            ],
            "problemMatcher": "$msCompile",
            "dependsOn": "clean"
        }
    ]
}

Launch a Debugging session in VS Code and you should see the default home page. Try to change the H1 tag from 'Welcome' to 'Hello' in your Views/Home/Index page and refresh to see if the changes are applied. It will only get applied if you stop the debugger and re-launch another debugging session.

** ** EDIT 2 ** **

Per Chen's suggestion, here's what my Program.cs looks like AFTER adding the AddRazorRuntimeCompilation line:

Program.cs

var builder = WebApplication.CreateBuilder(args);


builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation();


var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();



//NOTE: Error Occurs Here - type 'System.ArgumentNullException' occurred in System.Linq.dll: 'Value cannot be null.' 

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

Solution

  • I used the launch.json you provided for testing, and it did exactly what you said. I installed and registered Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation after changing the cwd, and the changed content will be applied after the page is refreshed. You can have a try:

    launch.json:

    {
        "version": "0.2.0",
        "configurations": [
            {
                //...
                "cwd": "${workspaceFolder}/TestMVCApp",
            },
            //...
        ]
    }
    

    Then install Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet Package:

    dotnet add TestMVCApp package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
    

    And in Program.cs:

    builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation();
    

    It worked for me.