How to make non c# cshtml changes and see results WITHOUT recompiling?

Viewed 588

Now that I'm on the latest and greatest version of everything, I found that my cshtml changes weren't being recognized until I stopped and restarted my application. Okay, no problem... I figured it out. I just had to enable the feature to allow Visual Studio to recompile my changes. Cool.

Well... not cool. Because it takes forever. Huge time waster. Before this, I could make all the cshtml changes I wanted and the results showed up just fine.

Any way to go back to how it was before? I honestly don't remember if C# code changes could be made and observed without recompiling before (I thought so, but maybe not). But I sure as heck know I didn't have to recompile to see my html/css/js changes. And I try to write as little C# in my views as possible... the vast majority of the changes I make are html/css/js.

When I need to be able to quickly hit F5 over and over in my browser to see tiny style changes until I get the page looking just right, this can literally eat up more than an hour of my life every day.

There must be a way to fix this?!

EDIT:

After writing this I realized that I was not experiencing this behavior in another .NET 5.0 solution. At the time, it was still very slow in the solution in question though. Just now I tried @PerryQian-MSFT's suggestion and it seemed to work like a charm... however I can't say whether it was actually this that resolved the issue or not as I then undid the change and it now still works quickly.

I may have applied an update to Visual Studio 2019 in the interim and there's an off chance they improved something, but I know that's unlikely. So not really sure whether applying Perry's steps triggered something that resolved the issue or whether it would have worked fine today had I not made any changes whatsoever.

1 Answers

CShtml file with asp net core projects is special and it cannot get the new changes when you modify the pages under debug process by default.

You have to do some changes to your project additionally and then you can get it:

1) install the nuget package called Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.

If your project targets to net 5.0, you have to install the nuget version 5.0.x, if it targets to net core 3.1, you have to install the nuget version 3.1.x.

2) add services.AddMvc().AddRazorRuntimeCompilation(); under ConfigureServices method from Startup.cs file.

Then, start debugging your project, make changes to cshtml file,(not stop debugging) refresh the browser page and you will get what you want.

Related