How to Create a Modular Blazor Web App

Since asking my question a lot has changed. Blazor is now a fully supported part of .net core and the tooling has massively improved.

Creating a modular Blazor app is now made easier through Razor class libraries

Razor Class Libraries allow you to create a project which contains

  • Static Assets (css, js etc)
  • Components
  • Blazor Pages (see below on how to make them work in a modular way)

Microsoft Docs: Razor class library

Making Razor Class Libraries Modular

Inside App.razor is the router for Blazor.

The router has a baked in parameter that takes an array of additional assemblies to look at and find any additional routes. Cleverly named AdditionalAssemblies

So you can use the "AdditionalAssemblies" parameter to point to a method which scans the referenced Razor Class Libraries for Pages.

    <Router AppAssembly="@typeof(Program).Assembly" AdditionalAssemblies="AssemblyScanning.GetAssemblies().ToArray()">
    <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>

Example Project

I have put together an example project on github as an example.

Github : ModularBlazor

No framework. Just the default Blazor bits, a couple of basic interfaces and some assembly scanning.