Search code examples
razorasp.net-core-mvc

Why do I need to enable razor runtime compile? Am I taking crazy pills?


As far as I know I've always been able to update cshtml files and immediately see changes (upon refreshing browser) without having to stop my project.

But as I am creating a new project I see this new option to enable this feature and I'm wondering what it is because, after all, I've always been able to do this. But as far as I can tell it does exactly what it says... as if I wasn't able to do this in the past?

But I also notice now that when I make a change to a razor page, it takes a looot longer to see the update as Visual Studio recompiles the project.

What... is... going... on?


Solution

  • As far as I know I've always been able to update cshtml files and immediately see changes (upon refreshing browser) without having to stop my project.

    Yes.Before asp.net core 3.0,you could do it by default.After asp.net core 3.0,you could install the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package to enable runtime compilation.

    1.Install the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package.

    2.Update the project's Startup.ConfigureServices method to include a call to AddRazorRuntimeCompilation:

    services.AddControllersWithViews().AddRazorRuntimeCompilation();
    

    Reference:

    https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?view=aspnetcore-5.0&tabs=visual-studio#enable-runtime-compilation-in-an-existing-project

    Update:

    From asp.net core 3.1,you could select the Enable Razor runtime compilation checkbox in the Create a new ASP.NET Core web application dialog:

    enter image description here