'Compilation error after publishing individual Razor page. Persists after publishing entire project. ("Compilation references may be missing")

Today I was working on a .NET Core 3.0 project (VS 2019). I added a CSS class to a <span> on one of my pages. That's it. Nothing else. I figured publishing only that file would be appropriate given how small the change was. I right clicked the file and selected "Publish". It published the .cshtml and .cshtml.cs pages together and finished in just a couple seconds.

Upon doing so, the page now won't compile in my production application.

One or more compilation references may be missing. If you're seeing this in a published application, set 'CopyRefAssembliesToPublishDirectory' to true in your project file to ensure files in the refs directory are published.

Every other page in the application is unaffected. It's specifically this one page that no longer works, even after publishing the entire project.

I've made the suggested changes:

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <CopyRefAssembliesToPublishDirectory>true</CopyRefAssembliesToPublishDirectory>
  </PropertyGroup>
  • I cleaned, rebuilt, and did a full publish of the entire project, but the error did not go away.

  • I attempted to publish only this single page after enabling the suggested setting in my project file.

  • I've restarted the Web Service in Azure multiple times.

  • Excluded the file, publish, re-add the file, publish.

  • Changed the page model name.

The error persists.

I'll include the entirety of the error below.

One or more compilation references may be missing. If you're seeing this in a published application, set 'CopyRefAssembliesToPublishDirectory' to true in your project file to ensure files in the refs directory are published.

The type or namespace name 'IndexModel' could not be found (are you missing a using directive or an assembly reference?)
+
        public global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<IndexModel> Html { get; private set; }
The type or namespace name 'IndexModel' could not be found (are you missing a using directive or an assembly reference?)
+
        public global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<IndexModel> ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<IndexModel>)PageContext?.ViewData;
The type or namespace name 'IndexModel' could not be found (are you missing a using directive or an assembly reference?)
+
        public IndexModel Model => ViewData.Model;
The type or namespace name 'IndexModel' could not be found (are you missing a using directive or an assembly reference?)
+
        public global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<IndexModel> ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<IndexModel>)PageContext?.ViewData;

EDIT:

I've found that I can resolve the issue by renaming the page, but unfortunately that is not a viable solution.



Solution 1:[1]

As of now, my solution was to delete the entire application from App Service (using the FTP credentials provided in the Azure Portal) and re-publish.

Still leaving this open for a better solution though.

Solution 2:[2]

I had this issue and what solved it for me was the relatively simple finding where the file had been uploaded to on the remote server (it had created a pages/x.cshtml file in the site's root directory) and deleting it. When you publish through build and not the solution explorer these files are not put in to the same place.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2