Example of a Razor Class Library component hosted in multiple environments
The shared component is a simple self-contained razor class library (RCL) that performs a simple search against the Guardian Api and displays the results in a paged list. You will need to sign-up for a key although 'test' seem to work for low volume queries.
You just need to reference the RCL and can add the component like a normal blazor component.
You just need to reference the RCL and add the component like a normal Blazor component. There is an extension method 'AddGuardianServices' in the component that needs to be added and also an appsettings.json has been added with an 'IsBlazorHosted' to disable default bootstrapping when hosting in aspnet .
There are a few steps to follow. This is a good guide Blazor Hosting.
- Create a simple wrapper component in the blazor application to wrap the RCL. I have called it GuardianShared.
- Reference Nuget package Microsoft.AspNetCore.Components.WebAssembly.Server
- Add <script src="_framework/blazor.webassembly.js"></script> to Layout page or page that is hosting the Blazor component.
- Make sure that builder.RootComponents.Add("#app") is disabled in the blazor application.
- Update program.cs with following changes in green
- The component itself is rendered as follows in the page