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:
.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>
dotnet restore
dotnet clean
and dotnet build
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
Pages
and Areas\Identity\Pages
Program.cs
file I have the following lines related to razor:
services.AddRazorPages().AddRazorRuntimeCompilation();
app.MapRazorPages();
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 pathmyproject\bin\debug\net6.0\publish
where the razorpages are inlcuded and the ui looks the way it should.
The answer was pretty simple but I didn't come across that. I found out that app.UseStaticFiles();
in the Program.cs should serve the wwwroot
folder but it somehow didn't. I than realized that you need one extra step so that UseStaticFiles
works and that is by adding the following lines to your .csproj file:
<ItemGroup>
<None Include="wwwroot\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>