Is it possible to have routing components in a separate project from the client application in Blazor WASM?

Viewed 204

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:

  1. 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.Web SDK for PRC
  2. In the PRC create a routed component and an empty class called Export
  3. Add a reference to the PRC in the WASM app
  4. In the web application, modify the App.razor such that the Router has an AdditionalAssemblies attribute 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,
  };

}
  1. In the assembly collection add the assembly to the PRC through its Export class (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.

1 Answers

I just fixed my Razor Class Library that created the same issue. I mimicked a library I knew worked https://github.com/Blazorade/Blazorade-Teams/blob/master/Blazorade.Teams/Blazorade.Teams.csproj

My csproj now looks like:

<Project Sdk="Microsoft.NET.Sdk.Razor">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <GeneratePackageOnBuild>false</GeneratePackageOnBuild>
  </PropertyGroup>

  <ItemGroup>
    <SupportedPlatform Include="browser" />
  </ItemGroup>
  
</Project>

In this project no @page directives are being used so I'm guessing its not yet possible. https://github.com/Blazorade/Blazorade-Teams/blob/master/Samples/TeamsTabAppClient/Pages/Login.razor

Add a component to the WASM project with the @page directive and use the component from the RCL to render the page.

Related