8

I am trying to get Razor runtime compilation working in an MVC Core 3.1 web application for Razor pages, but it's not working for me. It's frustrating as I have several previous MVC Core web applications across several releases (2.0, 2.1, 3.1) using controllers and views that work as expected.

What isn't working?

When publishing the project, I am expecting to see a /pages folder in the publish output with my .cshtml files. There isn't one, and there are no .cshtml files anywhere in the publish folder.

What does work?

The website works just fine, serving pages as expected, so the pages are compiled into the WebApplication3.1.Views.dll assembly correctly. No issues here.

What have I tried?

To reproduce, I created a new MVC project with Razor runtime compiliation enabled at creation, following the instructions in https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?view=aspnetcore-3.1&tabs=visual-studio. I can confirm the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.dll assembly is present in the published output, and services.AddRazorPages().AddRazorRuntimeCompilation() is called from the Startup.ConfigureServices method. Having looked at the docs, and several StackExchange answers, this should be enough to enable runtime compilation. the .csproj file looks like this:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <RootNamespace>WebApplication3._1</RootNamespace>
    <CopyRefAssembliesToPublishDirectory>false</CopyRefAssembliesToPublishDirectory>
  </PropertyGroup>

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

I then added <Target> elements to the .csproj file to get the pages in to the publish folder, which worked, but when I deployed and changed a .cshtml file I then got an exception that the Microsoft.AspNetCore.Antiforgery.dll couldn't be found. So I can see that runtime compilation kind of works, but is broken.

At this point this seems to be more complicated than it should be, considering that in other MVC Core 3.1 controller/view based projects of mine it just worked. Am I missing something really obvious?

I have also looked at other StackExchange questions, this one in particular, but it says essentially the same thing:

.NET Core 3.0: Razor views don't automatically recompile on change

I am using Visual Studio 2019 Professional version 16.6.4

RobA
  • 128
  • 1
  • 7
  • Could it be as simple as adding your pages folder manually from the solution explorer? See [this answer](https://stackoverflow.com/questions/62155966/xml-documentation-not-found-when-publishing-a-net-framework-web-api). – Yosef Bernal Jul 17 '20 at 10:34
  • @YosefBernal Thanks, although that doesn't help me. The website works just fine and the pages are included as compiled views OK, I just can't get the uncompiled .cshtml pages to be included. There doesn't appear to be an option to include uncomplied pages in the output. – RobA Jul 17 '20 at 10:38
  • It definitely works (I've just tried it on my 3.1 project). The views dll will automatically be recompiled when a page is changed. There is no need to have the /pages folder copied to the output. – Neil Jul 17 '20 at 11:03
  • Hi @Neil, just to confirm you've got it working for published output and deployed to IIS? It works just fine within Visual Studio but as I said above I can't even get the .cshtml pages to show up in the publish folder to edit them on the server. – RobA Jul 17 '20 at 12:11
  • The pages WONT turn up on the server because they are compiled into a DLL ! Why do you think they need to be? Why are you trying to edit them on the server? You edit and continue from VS during debugging not once it's been deployed. – Neil Jul 17 '20 at 20:11
  • Let's start again. Everything works when deployed, but you think it's not right because there are no view files? There are no view files because it's compiled into a DLL in 3.1, not separate cshtml files like it was in previous versions. – Neil Jul 17 '20 at 20:15
  • Same issue here. Seemed to just happened randomly, as mentioned [here](https://github.com/dotnet/aspnetcore/issues/20542). The only thing I know changed is a new insider build update 20175 was installed. Are you on the Insider builds? – Jarem Jul 25 '20 at 20:01

1 Answers1

4

It looks like you need the RazorCompileOnPublish property, which you can set in your .csproj file:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <RazorCompileOnPublish>false</RazorCompileOnPublish>
    ...
  </PropertyGroup>

  ...
</Project>

Setting RazorCompileOnPublish to false should be all that's needed, but I suspect you don't need that CopyRefAssembliesToPublishDirectory property in your .csproj.

Kirk Larkin
  • 84,915
  • 16
  • 214
  • 203