blazor-server-siderazor-component-libraryblazor-routing

Set home page from Razor Component Library DLL on Blazor Server site


I am fairly new to Blazor and I'm trying to re-use several pages from a Razor Component Library project as a reference in a Blazor Server App. I have tried several things based on articles and answers within stackoverflow. I can't seem to get it to work. I must be missing something simple.

I have a RCL project that is built into a dll and pulled into a Blazor server app from an Azure DevOps archive. I add as a reference into the server project. I have updated Routes.razor to reference the 'AdditionalAssemblies' which is what I was to understand what allowed for this.

Here is the updated line in Routes.razor:

<Router AppAssembly="@typeof(Program).Assembly" AdditionalAssemblies="new[] { typeof(Default).Assembly}">

The Default.razor component is in the RCL dll and contains the following code:

@page "/"

 @code {
    protected override async Task OnInitializedAsync()
    {
        //Root Redirect code will go here
        SiteDefaultRedirect siteDefaultRedirect =         BusinessSingleton.SiteDefaultRedirect.GetBySiteID(Int32.Parse(config["Site:ID"]));
        navManager.NavigateTo(siteDefaultRedirect.URL, true);
    }
 }

The purpose of this page is for any traffic hitting the root of the Web App to look up a redirect link in a database to go to the active path/page with tracking details for monitoring.

I tested this code in a Home.razor page in a test Blazor server app. It does exactly what I want but I need it to be a part of every site I run long term. I want it to be easily re-usable so I moved it into the RCL as Default.razor and removed Home.razor from the Sever app I now get a 404 error when loading the site root. I have seen several examples that have a component library hosted as a shared project inside the Blazor project itself and it seems to work fine but when I try within a separate DLL it can't seem to find the page.

Thanks in advance for any help!!


Solution

  • I had exactly the same issue and found your post. I fiddled a bit around and found this issue: https://stackoverflow.com/a/78203814/1561071

    So it works for me if I change this:

    app.MapRazorComponents<App>()
        .AddInteractiveServerRenderMode();
    

    to

    app.MapRazorComponents<App>()
        .AddInteractiveServerRenderMode()
        .AddAdditionalAssemblies(typeof(HomePageLibraryPage).Assembly);
    

    Hope it works for you as well