234

I'm building a .NET Core MVC on the latest version 2.2. I have a problem when I make changes to the CSHTML file and refresh the page, my changes are not reflected in the browser. I have to restart the project in order to see my changes. This has been happening for a while now so I'm not exactly sure what change caused this issue.

I've tried using the chrome's "Empty Cache and Hard Reload" as well as other browsers to no avail. This happens on Windows and Mac using both Visual Studio for Mac and VS Code

In a default .Net Core project it works fine so it must be something in my project that changed along the way. I'm wondering where I need to start in order to debug this issue? I've tried commenting out almost everything in my Startup.csand Program.cs with no resolution.

kevskree
  • 4,442
  • 3
  • 24
  • 32
  • I'm encountering the same MissingMethodException you mentioned below... Did you ever figure it out? If, so could you please answer your question? – IEnjoyEatingVegetables Aug 20 '19 at 19:59
  • For use with Rider and/or Razor Class Libraries (RCL), [see this answer](https://stackoverflow.com/a/58289831/107625). – Uwe Keim Nov 15 '20 at 11:28
  • Wow. Didn't see that coming. A real bummer. After 2 years this is upvoted only 147 times. Makes you wonder who's building apps with .NET Core... – dpant May 31 '21 at 07:03
  • Make 100% sure you are actually looking at the right area of code. I wasted many hours on this stupid issue only to find out it really was updating my code. – Post Impatica Mar 06 '23 at 13:14

17 Answers17

439

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:

  1. Add Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package to the project

  2. Add the following in Startup.cs:

    services.AddControllersWithViews().AddRazorRuntimeCompilation();
    

Here's the full explanation for the curious.

Alexander Christov
  • 9,625
  • 7
  • 43
  • 58
  • 2
    This didn't work for me for whatever reason. I'm on Mac if that makes a difference. – Nakul Tiruviluamala Oct 10 '19 at 03:50
  • @Nakul what is .net core version you use? – Alexander Christov Oct 10 '19 at 03:58
  • When I used this command: ls /usr/local/share/dotnet/shared/Microsoft.NETCore.App/ I seemingly got two answers: 2.1.13 and 3.0.0 – Nakul Tiruviluamala Oct 10 '19 at 07:47
  • @Nakul this is what you have installed, which one is your app's .net core version? – Alexander Christov Oct 10 '19 at 07:53
  • Awesome! Totally works for me on ASP.NET Core 3.0. I ended up with the following line: `services.AddMvc().AddControllersAsServices().AddRazorRuntimeCompilation().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);` – mvdgun Oct 18 '19 at 03:03
  • 8
    This worked great for me on Mac and ASP.NET Core 3.1. – user3071284 Dec 16 '19 at 22:31
  • services.AddControllersWithViews().AddRazorRuntimeCompilation(); – M Komaei Feb 29 '20 at 09:06
  • 4
    Thx for this hint; worked for me on .Net Core 3.1.2 – jawa Mar 04 '20 at 11:51
  • Thanks for the hint and for the link to the article on MSDN! I've followed the [conditionally enable runtime compilation](https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?view=aspnetcore-3.0#conditionally-enable-runtime-compilation) part of it and it worked well. Judging by the time it takes now to recompile a fairly simple view, I would highly recommend this way instead of enabling it always. – Sergey Kudriavtsev Mar 09 '20 at 00:06
  • 68
    Wowsers! 15 years working in .NET and it used to be easy. 1 day working with .NET Core and everything seems broken. And seriously - who decided it was a good idea to make HTML compiled and not changeable at runtime by default, then make developers hunt for a solution? – Jason Snelders May 09 '20 at 11:22
  • I get this error message - Package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation 6.0.0-preview.4.21253.5 is not compatible with netcoreapp3.1 – Anil P Babu Jun 01 '21 at 08:09
  • Looks like Startup.cs is the place to go for everything in .Net Core :) – Tejasvi Hegde Aug 13 '21 at 06:25
  • Worked for me on .NET 5 – Mehdi Daustany Sep 26 '21 at 23:05
  • Thanks ! Working on Windows Visual Studio 2019 Versión 16.11.3 and .net core 3.1.1 – Jesus Mostajo Sep 27 '21 at 10:37
  • Wored for me on .Net 5 with VS 2019 16.11.5 - What a pain! – JClarkCDS Oct 26 '21 at 22:36
  • This one has 20 prerequisites Packages OMG – Cătălin Rădoi Nov 13 '21 at 16:44
  • services.AddMvc(options => options.EnableEndpointRouting = false).AddRazorRuntimeCompilation(); -- this worked for me in Startup.cs. The ASPNETCORE_HOSTINGSTARTUPASSEMBLIES entry in launchSettings.json didn't do anything. FWIW can't seem to get Hot Reload to work either. in .Net 6. – George Beier Dec 03 '21 at 03:48
  • 1
    Thanks a lot! I spent too many hours debugging and trying different solutions since we upgraded our project from 2.x to 3.1 and the views stopped working. – Saeed Prez Jan 12 '22 at 14:01
  • 3
    with .net 6 on VS2022 this also works great! – Oded Dahari Jan 21 '22 at 21:22
  • .NET 7 on VS 2022 works – Pierre Feb 25 '23 at 17:40
  • 1
    The "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" NuGet package targets .NET. You'll need it to match your application's target framework. This fixed it for me. – Eli Mar 24 '23 at 03:28
  • Matching the version did for me too "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version 6.0.19, NET6 and VS2022 – Rizwan Jul 01 '23 at 23:48
104

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.

Max
  • 9,220
  • 10
  • 51
  • 83
Chris Aitchison
  • 1,056
  • 1
  • 6
  • 2
  • 2
    Thank you. However, it appears that when I enable this, make a change in the html, and then refresh, I get the following exception: ```MissingMethodException: Method not found: 'Microsoft.Cci.IMethodReference Microsoft.Cci.ICustomAttribute.Constructor(Microsoft.CodeAnalysis.Emit.EmitContext)'. Microsoft.CodeAnalysis.CSharp.Symbol.Microsoft.CodeAnalysis.ISymbol.GetAttributes()``` Any idea on what this exception means? All of the other pages load fine. Only when I edit a file and refresh do I get this error – kevskree Dec 19 '18 at 05:33
  • 3
    This worked for me. To set it depending on environment, add IHostingEnvironment to the Startup method and persist in a property. Then use something like `services.AddMvc().AddRazorOptions(options => options.AllowRecompilingViewsOnFileChange = _env.IsEnvironment("MyEnvironment"));` – KuriosCurious Apr 18 '19 at 20:49
  • 1
    thanks it worked. However I think it's pretty awkward and silly that Microsoft doesn't officially announce big changes like this. – Code_Worm Aug 12 '19 at 18:04
  • @kevskree same thing is happening to me – IEnjoyEatingVegetables Aug 20 '19 at 20:00
  • This is not working for asp.net core version 3.1 . Please suggest for 3.1 – Mayank Gupta Jan 19 '20 at 15:59
  • 1
    @Mayank Gupta: see Alexander Christov's answer below (https://stackoverflow.com/a/57637903/198990). His answer worked for me (3.1). – Sandor Drieënhuizen Feb 18 '20 at 15:09
  • this worked, but weird that this is project specific, and have to add the package each time we create a new project. I thought that there would be any ide tweak or some property change before finding this solution. – Ganesh Kodiganti Apr 28 '22 at 02:22
71

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...
Jeremiah N
  • 811
  • 6
  • 4
  • 4
    This is the best answer without reading the article posted by Alex, which is worth the read. – jon.r Feb 07 '20 at 15:23
46

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:

  1. Install the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package.
  2. Set next environment variables (for example via environmentVariables section in launchSettings.json):
    • ASPNETCORE_ENVIRONMENT to "Development".
    • ASPNETCORE_HOSTINGSTARTUPASSEMBLIES to "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation".
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • 4
    This should be the accepted answer; it allows for the desired functionality without needing to alter the Startup file. – Tom Regan Feb 01 '21 at 15:53
  • 1
    And if you upgrade a project from .NET 5 to .NET 6, you will need to update `Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation` from v5.x to v6.x as well or you won't be able to see changes in Razor Views or Pages as you make them. – Tommy Williams Feb 11 '22 at 18:07
  • Hello... I am having this same problem but it did not work. The bad side effect is that all accented vowels appear ugly in the browser. If I remove `Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation` from launchSettings.json file, accented vowels appear correct again. It seems there is no solution to the view compilation problem. – jstuardo Nov 03 '22 at 19:40
19

first of all install the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation using nuget manager after that add below code into your startup.cs

services.AddRazorPages().AddRazorRuntimeCompilation();

net6.0 also this works.

builder.Services.AddRazorPages().AddRazorRuntimeCompilation();
Arun Prasad E S
  • 9,489
  • 8
  • 74
  • 87
Ishara Samintha
  • 460
  • 6
  • 8
13

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();
Clint Warner
  • 1,265
  • 1
  • 9
  • 25
13

For those using Net core 3.0 or greater

  1. Go to Tools → Nuget package manager → Manage nuget pakages for solution

  2. move to browse tab to browse from internet

  3. search for RuntimeCompilation Click on Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation

  4. install it on your intended projects the current stable version

  5. open Startup.cs file

  6. go to void method ConfigureServices

  7. add line: services.AddControllersWithViews().AddRazorRuntimeCompilation();

  8. you are DONE

Rerun and see. Now you can refresh your views or pages.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
GeraGamo
  • 226
  • 3
  • 6
5

Using .net core 2.2 running app with command dotnet watch run the project is restarted after every change

nanquim
  • 1,786
  • 7
  • 32
  • 50
5

Below helped me when views were in separate project.

if(HostingEnvironment.IsDevelopment()){ // only in development (optional)
    services.AddMvc().AddRazorOptions(o => {
        o.FileProviders.Add(new PhysicalFileProvider(PATH_TO_PROJECT));
    });
}
smoq
  • 130
  • 2
  • 6
  • 1
    This is the only answer that worked for me when working with Razor Class Libraries. Cheers! – Sipke Schoorstra Sep 09 '19 at 18:51
  • Using Core 3.1 followed these directions, installed version 3.1.16 and received error: 'IMVcBuilder' does not contain a definition for 'AddRazorRuntimeCompilation' and no extension method 'AddRazorRuntimeCompilation' accepting a first argument of type 'IMVcBuilder' could be found. – Clarence Jun 27 '21 at 06:27
5

I had the same issue while working on a .NET 6 MVC Web App.

I installed Microsoft.AspNetCore.Mvc.Razor.Runtime.Compilation from NuGet Package Manger then added .AddRazorRuntimeCompilation(); after

builder.services.AddControllersWithViews();

so that it looks like this

builder.services.AddControllersWithViews().AddRazorRuntimeCompilation();

and it worked!

Hope this helped.

Saeed Adam
  • 59
  • 1
  • 3
1

I was able to solve this problem in Rider by adding the ASPNETCORE_ENVIRONMENT=Development environment variable.

user1613512
  • 3,590
  • 8
  • 28
  • 32
1

There are two ways to resolve this issue:

1. Check the permissions of folder in which your .sln file present.There may be a problem with file access permissions as Visual studio may not be to access files when IIS express server is running, so to reflect new .cshtml changes each time you need to restart the server,so I suggest edit the folder access permissions by:

Right click on folder->properties->security->click on edit button -> check all options->save.

Restart Visual studio to see changes.

If this does not work then use 2 option.

2.In your project in startup.cs file add this below line ConfigureServices() in method :

services.AddMvc().AddRazorOptions(options => options.AllowRecompilingViewsOnFileChange = true);

Omkar
  • 110
  • 3
0

Are you absolutely sure you are using 2.2? Check your csproj because it might be this bug https://github.com/aspnet/Razor/issues/2466 You could try turning off RazorCompileOnBuild more info https://learn.microsoft.com/en-us/aspnet/core/razor-pages/sdk?view=aspnetcore-2.1#properties

Wanton
  • 800
  • 6
  • 9
0

I had a similar problem upgrading from .net Core 3 to .net 5.0

Problem was due to old dependency in Telerik controls that we could not change.

Fixed by changing references in the .csproj file

<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="3.8.0" />

to

<PackageReference Include="Microsoft.CodeAnalysis" Version="3.8.0" />

(your version may be different)

Ruskin
  • 5,721
  • 4
  • 45
  • 62
0

In Visual Studio 2022 Preview, it seems there is an option called Hot Reload for this purpose.

enter image description here

It seems to be available in Visual Studio 2019 too.

With Hot Reload you can now modify your apps managed source code while the application is running, without the need to manually pause or hit a breakpoint. Simply make a supported change while your app is running and in our new Visual Studio experience use the “apply code changes” button to apply your edits.

https://devblogs.microsoft.com/dotnet/introducing-net-hot-reload/

Karr
  • 395
  • 5
  • 9
0

I had same issue and after trying few of the above suggested options (including the one marked as answer) without success.

So, I decided to trash Startup.cs and build the Program.cs properly. Then my Hot Reload started working.

Aweda
  • 323
  • 1
  • 4
  • 15
0

In my case I had Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation nuget installed, but it was version 5.x.x. Updating to 6.x.x resulted in the pages properly updating when changed. So make sure you update!

spiral
  • 47
  • 8