title: Razor Page Runtime Compilation not working
body: <p>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.</p>
  <h2>What isn't working?</h2>
  <p>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.</p>
  <h2>What does work?</h2>
  <p>The website works just fine, serving pages as expected,  so the pages are compiled into the <code>WebApplication3.1.Views.dll</code> assembly correctly. No issues here.</p>
  <h2>What have I tried?</h2>
  <p>To reproduce, I created a new MVC project with Razor runtime compiliation enabled at creation, following the instructions in <a href="https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?view=aspnetcore-3.1&amp;tabs=visual-studio" rel="noreferrer">https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?view=aspnetcore-3.1&amp;tabs=visual-studio</a>. I can confirm the <code>Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.dll</code> assembly is present in the published output, and <code>services.AddRazorPages().AddRazorRuntimeCompilation()</code> is called from the <code>Startup.ConfigureServices</code> method. Having looked at the docs, and several StackExchange answers, this should be enough to enable runtime compilation. the .csproj file looks like this:</p>
  <pre><code>&lt;Project Sdk=&quot;Microsoft.NET.Sdk.Web&quot;&gt;
    &lt;PropertyGroup&gt;
      &lt;TargetFramework&gt;netcoreapp3.1&lt;/TargetFramework&gt;
      &lt;RootNamespace&gt;WebApplication3._1&lt;/RootNamespace&gt;
      &lt;CopyRefAssembliesToPublishDirectory&gt;false&lt;/CopyRefAssembliesToPublishDirectory&gt;
    &lt;/PropertyGroup&gt;
  
    &lt;ItemGroup&gt;
      &lt;PackageReference Include=&quot;Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation&quot; Version=&quot;3.1.4&quot; /&gt;
    &lt;/ItemGroup&gt;
  &lt;/Project&gt;
  </code></pre>
  <p>I then added <code>&lt;Target&gt;</code> 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 <code>Microsoft.AspNetCore.Antiforgery.dll</code> couldn't be found. So I can see that runtime compilation kind of works, but is broken.</p>
  <p>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 <em>it just worked.</em> Am I missing something really obvious?</p>
  <p>I have also looked at other StackExchange questions, this one in particular, but it says essentially the same thing:</p>
  <p><a href="https://stackoverflow.com/questions/54600273/net-core-3-0-razor-views-dont-automatically-recompile-on-change">.NET Core 3.0: Razor views don&#39;t automatically recompile on change</a></p>
  <p>I am using Visual Studio 2019 Professional version 16.6.4</p>