dotnet / razor

Compiler and tooling experience for Razor ASP.NET Core apps in Visual Studio, Visual Studio for Mac, and VS Code.
https://asp.net
MIT License
489 stars 190 forks source link

Razor class library views runtime compilation, missing step in documentation ? #10780

Open CurlyFire opened 3 weeks ago

CurlyFire commented 3 weeks ago

Goal

I am trying to edit at runtime a .cshtml view defined in a razor class library and see the effects of my modifications without restarting the application, thus achieving runtime compilation of razor views. I have followed the instructions given here:

https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?view=aspnetcore-8.0&tabs=visual-studio#enable-runtime-compilation-for-a-razor-class-library

Expected results and actual results

I was expecting the edited view (defined in razor class library) to be refreshed, however when I edit a .cshtml and save it, nothing changes when I force a refresh (ctrl-f5) of the browser.

What I tried basically:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews()
    .AddRazorRuntimeCompilation();
builder.Services.Configure<MvcRazorRuntimeCompilationOptions>(options =>
{
    var libraryPath = Path.GetFullPath(
        Path.Combine(builder.Environment.ContentRootPath, "..", "Bug.RazorPages.RazorClassLibrary"));

    options.FileProviders.Add(new PhysicalFileProvider(libraryPath));
});

Still it did not work, so I tried this:

And now it works.

So basically my issue is: Is this the correct way ? If not what is required ? Either way, I think the documentation should be updated to be more clear. Thanks!

Sample github projet

I have created a sample project on github demonstrating the problem:

https://github.com/CurlyFire/Bug.RazorPages

Steps to reproduce:

phil-allen-msft commented 3 weeks ago

It sounds like you are looking to make edits to the pages while debugging the application and see the edits take effect. This is normally accomplished using "Hot Reload" rather than "Runtime Compilation". In order to get you the answers to help this forward, can you tell us what editor you are using to make the edits and what lead you to use Runtime Compilation rather than Hot Reload?

CurlyFire commented 2 weeks ago

@phil-allen-msft : I am using Visual Studio directly to edit the files. The reason I was using runtime compilation is that was the technique that was used on the project before I modified it to use a razor class library, so I didn't question the technique.

phil-allen-msft commented 1 week ago

@CurlyFire , given your current setup, does "Hot Reload" get you your desire to do iterative development under the debugger in Visual Studio?

CurlyFire commented 1 week ago

@phil-allen-msft Yes it does