Search code examples
c#.netasp.net-corerazorrazor-pages

Runtime Compilation of Razor Pages Does Not Work .NET 8 VS2022


I am having a hard time enabling runtime compilation of Razor pages under IIS v10.0, using Visual Studio 2022 17.8.8, .NET 8.0.103 (Windows 11).

I have followed the instructions at https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?view=aspnetcore-8.0&tabs=visual-studio and added the required nuget package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation (v8.03). Configured the services as instructed, except I am not limiting the runtime compilation to only the dev environment. I want it active in the published site (yes I know what I am doing, I am aware of the potential performance problems, etc.)

Once the site is published to a directory on local IIS, it works per se, but the runtime compilation doesn't. There are no .cshtml pages copied upon publishing and so nothing to modify in place.

OK, so I tried changing their "Copy to Output Directory" setting in the Solution Explorer to "Copy always". They then get copied upon publishing, but are inert - changing their content has no effect on the application in the browser. Apparently the pages still get compiled into a static assembly upon publishing.

I looked at some previous answers, for example this: Razor Page Runtime Compilation not working (accepted solution concerning RazorCompileOnPublish setting does not work) and this .NET 6 MVC enabling razor runtime compilation breaks program.cs (different, unrelated problem) and their suggestions didn't help me.

I have also enlisted Claude (AI) and it had some suggestions, none of which worked. I need some human intelligence to help me out here, please.

I should mention that I think I remember doing it before, with previous versions of .NET Core and VS, and I don't recall it being such a pain. This should be simple: Razor Pages are presumably meant to be dynamic and easy to setup, as opposed to MVC.

<rant>I need a lightweight runtime-compiled, essentially a scripting, solution to creating dynamic websites. I need to be able to change a single file on the web server and have it reflected upon reload of the page, just like you would see with .html files. This is becoming progressively more difficult with each .NET update. Classic ASP anyone? If it weren't horrible VBScript I would still use it, but at this point, I was hoping for something up-to-date and modern, though. ASP.NET Web Pages 2 (from ca. 2011) fit the bill, but they have been deprecated (they use .NET 4.x/C# v5 and are no longer being updated).</rant>


Solution

  • I have started completely from scratch and

    1. Created a totally new "ASP.NET Core Web App (Razor Pages)" project, called RazorPages.RuntimeCompilation.Test,
    2. Installed the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation (this time, 8.0.4), and
    3. Added the .AddRazorRuntimeCompilation(); call to program.cs.
    4. Added <RazorCompileOnPublish>false</RazorCompileOnPublish> to the project .csproj file, inside the <PropertyGroup node.

    The Razor .cshtml views now get copied into Pages folder in the site, rather than being compiled to RazorPages.RuntimeCompilation.Test.dll assembly.

    I am not sure what was wrong with the previous project (I followed the same steps) but it works as expected now with the new project. Maybe it was the new version of Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation that appeared in the meantime.