DEV Community

Cover image for Turn on runtime compilation for modified views in ASP.NET Core
Emanuele Bartolesi
Emanuele Bartolesi

Posted on • Edited on

10

Turn on runtime compilation for modified views in ASP.NET Core

As a seasoned ASP.NET developer I used to modify the views when I working on CSS/HTML, hit F5 and see what is changed on the UI.
With ASP.NET Core it's not possible by default because the views are compiled by default and they are included in the bin folder with the others dll files.
The most important advantage of this new behavior is one of the ASP.NET Core feature: performance.
In the other hand you can create, for instance, a lot of default views, put them into a class library and reuse that views in many projects.

But it's still possible to compile views when the files are changed.

Turn on runtime compilation

There are few steps to make it happens.

  • Find and install the NuGet package called Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
  • In the ConfigureService method, replace the content with this line:
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews()
                .AddRazorRuntimeCompilation();
        }
Enter fullscreen mode Exit fullscreen mode

Now you can press F5, change something in the views and dotnet recompile them at runtime.
But if you don't want the services in the production environment, you can use the conditional compile options.
Let's see how.

Compile the views only for Debug

To achieve this goal, we can use an out-of-the-box feature offered by Visual Studio and .NET.
Just add "#if (DEBUG)" in the code and add the runtime compilation only if you on debug.
In this cause you have performance in the production environment because you don't use this service but more productivity when you are on debug.

        public void ConfigureServices(IServiceCollection services)
        {
            var mvcviews = services.AddControllersWithViews();

            #if (DEBUG)
            mvcviews.AddRazorRuntimeCompilation();
            #endif
        }
Enter fullscreen mode Exit fullscreen mode

Enjoy your compiled views!!!

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (1)

Collapse
 
luca7993 profile image
Luca7993

Hi,
I have a razor class library where I have razor views on it.
Using the AddRazorRuntimeCompilation works only for razor views inside web project and not on views on referenced razor class libary.
is there a way to make it work?

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

If this article connected with you, consider tapping ❤️ or leaving a brief comment to share your thoughts!

Okay