Moving Razor Pages and Layouts to RCL

116 Views Asked by At

I can't seem to get why the Blazor Server Web runs with a 404 when I try to move Components/Pages and Components/Layouts from the default Blazor Server Web project to a new RCL project. Everything is at default, .NET 8.0 is used, and one thing I picked up from tutorials is to add AdditionalAssemblies to the Router:

<Router AppAssembly="@typeof(Program).Assembly" AdditionalAssemblies="new[] {typeof(ComponentLib.Components.Layout.MainLayout).Assembly, typeof(ComponentLib.Components.Pages.Home).Assembly}">
    <Found Context="routeData">
        <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"/>
        <FocusOnNavigate RouteData="@routeData" Selector="h1" />
    </Found>
</Router>

Otherwise I have no idea why the 404, I tried moving the Pages dir to the root of RCL project, I tried setting copy-always for the pages and the layout files. When I "return/copy" the Components/Pages and Components/Layouts to the Blazor Web project, everything is back to normal, Hello World is displayed.

2

There are 2 best solutions below

0
steakoverflow On

It seems pages have to be in the Web project not in RCL:

https://github.com/dotnet/aspnetcore/issues/49432

0
Malcolm T. On

Not enough Rep to comment, and this isn't really worth an actual Answer, but...

You could switch to .NET 7, where it works fine. I use a shared RCL with most of my pages, components, services, and use it both in a MAUI .NET project and a Blazor Server project, no problems (beyond the norm).

To answer your problem with .NET 8: That was a bug in the .NET 8 Previews, and in RC-1, someone commented it was working: https://github.com/dotnet/aspnetcore/issues/48767#issuecomment-1718078998 with this code:

app.MapRazorComponents<App>()
    .AddAdditionalAssemblies(typeof(MainLayout).Assembly);

Reading further, it looks like it may have been slipped to .NET 9, but it wasn't clear if the above would still work or not. You can try.