C#C
C#12mo ago
4 replies
Casper

How to add razor pages to razor class library and reference from Blazor server project?

I am trying to place all my razor pages and components inside a razor class library to modularize the code to pack as a nuget package. I can get the components rendered but routing to pages just wont work.

I have added the assembly to be scanned in the blazor web project as such and referenced the project in the csproj file:

`csharp

@using System.Reflection
@using AppBlueprint.UiKit.Components.Pages

<Router AppAssembly="@typeof(Program).Assembly"
        AdditionalAssemblies="@AdditionalAssemblies">
    <Found Context="routeData">
        <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
    </Found>
    <NotFound>
        <MudText Typo="Typo.h6">Sorry, there's nothing here!</MudText>
    </NotFound>
</Router>

@code {
    private Assembly[] AdditionalAssemblies { get; } = new[] { typeof(AppBlueprint.UiKit.Components.Pages.Dashboard).Assembly };
    
    protected override void OnInitialized()
    {
        foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
        {
            Console.WriteLine($"Loaded Assembly: {assembly.FullName}");
        }
    }
}
`
Was this page helpful?