loading...

Refresh ASP.NET Core MVC view without reloading the application

alexzeitler profile image Alexander Zeitler ・1 min read

After doing no ASP.NET (Core) stuff for while, today I created an ASP.NET Core MVC 3.0 application using dotnet new mvc.

I started the application using dotnet watch run and I was surprised to see the whole application restarting even when I just changed a single character within a MVC View e.g. ./Views/Home/Index.cshtml.

The solution was to add this NuGet package to the project:

Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation

In the Startup.cs I had to change this line:

public void ConfigureServices(IServiceCollection services)
{
     services.AddControllersWithViews();
}

to this

public void ConfigureServices(IServiceCollection services)
{
     services.AddControllersWithViews()
         .AddRazorRuntimeCompilation();
}

Now the application doesn't have to restart for simple HTML/Razor content changes.

Happy coding 😉

Posted on Dec 11 '19 by:

Discussion

markdown guide
 

It would be nice if you somehow could add also live reloading, but that would need to do some trick since we are still doing server side pages here. We are getting closer to that nifty Webpack experience of both recompiling (that is packing again) and reloading. Great tip anyways! I could not do this without upgrading from .NET Core 3.0 to .NET Core 3.1 btw.