Is it possible to create Blazor pages inside RCL?
Asked Answered
A

1

12

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

Auxiliaries answered 30/3, 2020 at 12:52 Comment(1)
You can do this without a library, see https://mcmap.net/q/746505/-convert-webassembly-blazor-to-a-hosted-oneRewrite
A
31

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

Apices answered 30/3, 2020 at 22:44 Comment(1)
Amazing. I had been looking for ages. And to get the answer from a Leslie Nielsen character, no less!Maciemaciel

© 2022 - 2024 — McMap. All rights reserved.