Introduction
Is it possible to have routing components in a separate solution project from the Blazor client-side application?
I was able to accomplish this with the .NET 5 server-side variant and the client-side running on .NET Standard. Unfortunately, with the changes introduced with .NET 5, this seems to be no longer possible for the WASM application - I'm getting the following error:
BLAZORSDK1001 The project references the ASP.NET Core shared framework, which is not supported by Blazor WebAssembly apps. Remove the framework reference if directly referenced, or the package reference that adds the framework reference. REDACTED C:\Program Files\dotnet\sdk\5.0.103\Sdks\Microsoft.NET.Sdk.BlazorWebAssembly\targets\Microsoft.NET.Sdk.BlazorWebAssembly.Current.targets 635
How I'm doing this with WASM on .NET Standard 2.1:
My setup is very similar to the one used here (as of commit #1219363 on Nov 15, 2020): https://github.com/SamProf/MatBlazor/tree/master/src
How to replicate:
- Have a WASM application and a project containing Blazor routed components (let's call it PRC for short)
- I've chosen the
Microsoft.NET.Sdk.WebSDK for PRC
- I've chosen the
- In the PRC create a routed component and an empty class called
Export - Add a reference to the PRC in the WASM app
- In the web application, modify the
App.razorsuch that theRouterhas anAdditionalAssembliesattribute referencing a collection of assemblies:
@using System.Reflection
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly" AdditionalAssemblies="COMPONENT_ASSEMBLIES">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</CascadingAuthenticationState>
@code {
private static readonly Assembly[] COMPONENT_ASSEMBLIES =
{
typeof(Components.Auth.Web.Export).Assembly,
};
}
- In the assembly collection add the assembly to the PRC through its
Exportclass (see code above as reference)
Reasons for doing this:
- Testing the application with WASM and server side - sharing the routed components (pages)
- Allowing me to easily swap out a project with components for a different one to modify how the application looks
The question
- Is it possible to accomplish this in .NET 5 WASM?
- If so, how?
Thank you for your help in advance.