ASP.NET Core Razor Pages not Compiling Correctly in .NET 6

I’ve been working on an ASP.NET Core project using Razor Pages in .NET 6, but I’ve encountered an issue where my Razor Pages are not being compiled during the build process. That means that they dont appear in my output folder and I get a “raw ui” without any styles. I’ve tried several solutions, but none seem to work.

Here’s a summary of what I’ve tried:

  • added the following lines to the .csproj:
<PropertyGroup>
  <MvcRazorCompileOnBuild>true</MvcRazorCompileOnBuild>
  <RazorCompileOnBuild>true</RazorCompileOnBuild>
</PropertyGroup>

<ItemGroup>
  <None Update="Pages\**\*.cshtml">
    <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
  </None>
  <None Update="Areas\Identity\Pages\**\*.cshtml">
    <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
  </None>
</ItemGroup>
  • Restored NuGet packages using dotnet restore
  • Cleaned and rebuilt the project using dotnet clean and dotnet build
  • I checked if there is the path obj\Debug\net6.0\Razor after the building process, but it’s not.

Despite of those steps the problem persists, and I’m still unable to get my Razor Pages to compile correctly. Are there any additional steps or configurations I might be missing?

While running the project out of Visual Studio the UI looks fine. But if I try to run the executable from the myproject\bin\Debug\net6.0 path I see the “raw UI” without any styles. I appreciate any help or insights you can provide to troubleshoot and resolve this problem. Thank you in advance! I guess there is one magic step that fixes it, but I can’t figure it out.

My project setup

  • I have cshtml-pages under Pages and Areas\Identity\Pages
  • In the Program.cs file I have the following lines related to razor:
    services.AddRazorPages().AddRazorRuntimeCompilation();
    app.MapRazorPages();
  • This is a shorted screenshot of my solution manager:
    enter image description here

Edit
Here’s what I found out by accident. However, I don’t understand why it behaves this way and it still doesn’t solve my original problem. Publishing the project creates a folder with this path myproject\bin\debug\net6.0\publish where the razorpages are inlcuded and the ui looks the way it should.

  • that’s probably normal… the “wwwroot” folder is special and will be created when you publish. That contains your CSS files. Nothing in that folder is built into the executable/dlls… they are static assets.

    – 




Leave a Comment