How to share a Blazor WebAssembly component that is ASP.NET Core Hosted using a single Razor Class Library?

geoCode 1 Reputation point
2022-02-17T23:09:54.347+00:00

I have a functioning Blazor WebAssembly (WASM) component that I would like to share with other apps. The component is ASP.NET Core hosted and my solution contains multiple project files (client, server, shared). The client-side project (the component) interacts with Web API endpoints in a controller file in the server-side project to pull data from a SQL Server database.

What I am unsure of is how to best share a WASM component with multiple other projects in a ASP.NET Core hosted environment. Apparently, directly adding/referencing the client-side WASM project does not work and using Razor Class Libraries (RCLs) is the suggested recommended approach (reference). However, I have been unable to find information on using RCLs in a hosted environment where a component-specific controller is involved.

How can I best share my client-side Blazor WASM component including its server-side controller using a single Razor Class Library? Is this even possible?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,553 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,577 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 65,206 Reputation points
    2022-02-21T21:58:19.56+00:00

    as the WASM and server are separate projects, you probably need 3 projects. one for the shared code, one for the Blazor components, and one for the server project.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.