In ASP.NET Core 3.0 and higher, RazorViewEngineOptions.AllowRecompilingViewsOnFileChange
is not available.
Surprised that refreshing a view while the app is running did not work, I discovered the following solution:
Add Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package to the project
Add the following in Startup.cs
:
services.AddControllersWithViews().AddRazorRuntimeCompilation();
Here's the full explanation for the curious.
There was a change made in ASP.NET Core 2.2 it seems (and I can't find any announcements about this change). If you are not explicitly running in the 'Development' environment then the Razor Views are compiled and you will not see any changes made to the .cshtml
You can however turn off this using some config in your Startup class as follows.
services.AddMvc().AddRazorOptions(options => options.AllowRecompilingViewsOnFileChange = true);
For ASP.NET Core 3.0 and higher, see Alexander Christov's answer.
I've just created a new project using the latest ASP.NET MVC Core 3.1 template and I altered the following to get runtime recompilation enabled for Debug:
Reference NuGet package - Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.
Startup.cs - ConfigureServices(IServiceCollection services) WAS:
// stuff...
services.AddControllersWithViews();
// more stuff...
NOW:
// stuff...
var mvcBuilder = services.AddControllersWithViews();
#if DEBUG
mvcBuilder.AddRazorRuntimeCompilation();
#endif
// more stuff...
In addition to Alexander Christov's answer, since ASP.NET Core 3.1 you can enable the view compilation for development environment without changes to the Startup
file:
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
NuGet package.ASPNETCORE_ENVIRONMENT
to "Development".ASPNETCORE_HOSTINGSTARTUPASSEMBLIES
to "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation".You should just add this:
services.AddControllersWithViews();
to the ConfigureService method.
Be aware below code is not available in ASP.NET Core 3.1:
services.AddControllersWithViews().AddRazorRuntimeCompilation();
For those using Net core 3.0 or greater
Go to Tools → Nuget package manager → Manage nuget pakages for solution
move to browse tab to browse from internet
search for RuntimeCompilation Click on Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
install it on your intended projects the current stable version
open Startup.cs file
go to void method ConfigureServices
add line: services.AddControllersWithViews().AddRazorRuntimeCompilation();
you are DONE
Rerun and see. Now you can refresh your views or pages.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With