Search code examples
asp.net-coreblazorblazor-server-sideblazor-client-siderazor-class-library

Is it possible to create Blazor pages inside RCL?


This page explains how to create a Razor class library where to put shared components. I've tried to create a sample Blazor Server-Side project where its index.razor has a component that is defined inside the Razor class library. It works.

What I would like to do is develop a Blazor Server-Side application with the possibility to change the hosting model to WASM in the future. I was wondering if it is possible to create pages and components inside a shared RCL so, in that case, I can create controllers inside the Blazor Server-Side project and consume them from the Razor class library through Http calls. This would help me in the future if I want to change the hosting model to WASM.

Anyway, I've tried to create a Blazor page inside the RCL but when I try to call the URL written inside @page it doesn't reach.

I would like to ask you if it is possible to accomplish this behavior. If yes, what am I missing?

You can replicate my sample just creating a new Blazor Server-Side project, make it reference an RCL and then insert @page "/testpage" inside the Component1.razor


Solution

  • Yes, you can - you need to tell the Blazor Router where to look for pages

    <Router
        AppAssembly="typeof(Program).Assembly"
        AdditionalAssemblies="new[] { typeof(Component1).Assembly }">
        ...
    </Router>
    

    Documentation : https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/routing?view=aspnetcore-3.1#route-to-components-from-multiple-assemblies-2