Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
CurlyFire opened this issue Aug 22, 2024 · 4 comments

Comments

@CurlyFire
Copy link

CurlyFire commented Aug 22, 2024

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:

  • I created a razor class library with a controller and a view and 1 mvc host project (.net 8)
  • I added a project reference from host to razor class library
  • I added a nuget reference to Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation to the host project
  • I added the runtime compilation in the host and the fileprovider for the razor class library:
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:

  • I added the nuget package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation directly to the razor class library, not only the host project.

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:

  • Run the application in debug
  • Edit Bug.RazorPages.RazorClassLibrary/Views/RazorClassLibrary/Index.cshtml, save and refresh the page
  • Notice that nothing happens
  • Stop debugging
  • Add nuget package reference Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation directly to the razor class library
  • Run the application in debug
  • Edit Bug.RazorPages.RazorClassLibrary/Views/RazorClassLibrary/Index.cshtml, save and refresh the page
  • Notice that it refreshes
@phil-allen-msft
Copy link
Contributor

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
Copy link
Author

@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
Copy link
Contributor

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

@CurlyFire
Copy link
Author

@phil-allen-msft Yes it does

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants