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

70 views Asked by At

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 runtime-compiled, essentially a scripting, solution to creating dynamic websites. This is becoming progressively more difficult with each toolchain 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.</rant>

1

There are 1 answers

2
Fengzhi Zhou On

You could follow my steps.

1.Refer to the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation (v8.03) as you have done, and copy the cshtml file.

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="8.0.3" />
  </ItemGroup>

    <ItemGroup>
        <PageFiles Include="$(ProjectDir)\Pages\**\*.cshtml" />
    </ItemGroup>

    <!-- Copy .cshtml files from Pages folder after publishing -->
    <Target Name="CopyPageFilesAfterPublish" AfterTargets="Publish">
        <Copy SourceFiles="@(PageFiles)"
          DestinationFolder="$(PublishDir)\Pages\%(RecursiveDir)"
    />
    </Target>

</Project>

2.Add the service

builder.Services.AddRazorPages().AddRazorRuntimeCompilation();

3.Test

files copied

enter image description here

dynamic pages

enter image description here enter image description here