I am trying to implement Blazor (.Net8) hosted web app with lazy loading.
This this my project structure
Program.cs
using BlazorApp.Components;using Microsoft.AspNetCore.Components.WebAssembly.Services;var builder = WebApplication.CreateBuilder(args);builder.Services.AddScoped(typeof(LazyAssemblyLoader));builder.Services.AddRazorComponents() .AddInteractiveWebAssemblyComponents();var app = builder.Build();if (app.Environment.IsDevelopment()){ app.UseWebAssemblyDebugging();}else{ app.UseExceptionHandler("/Error", createScopeForErrors: true); app.UseHsts();}app.UseHttpsRedirection();app.UseStaticFiles();app.UseAntiforgery();app.MapRazorComponents<App>() .AddInteractiveWebAssemblyRenderMode() .AddAdditionalAssemblies(typeof(BlazorApp.Client._Imports).Assembly);app.Run();Routes.razor
@using BlazorApp.Client.Layout@using Microsoft.AspNetCore.Components.WebAssembly.Services@using System.Reflection@inject LazyAssemblyLoader assemblyLoader<Router AppAssembly="@typeof(Program).Assembly" OnNavigateAsync="OnNavigateAsync" AdditionalAssemblies="_lazyLoadedAssemblies"><Found Context="routeData"><RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" /><FocusOnNavigate RouteData="@routeData" Selector="h1" /></Found><NotFound><PageTitle>Not found</PageTitle><LayoutView Layout="@typeof(MainLayout)"><p role="alert">Sorry, there's nothing at this address.</p></LayoutView></NotFound><Navigating><p>Page loading...</p></Navigating></Router>@code{ private readonly List<Assembly> _lazyLoadedAssemblies = []; private async Task OnNavigateAsync(NavigationContext context) { var assemblies = context.Path switch {"dashboard" => await assemblyLoader.LoadAssembliesAsync(new[] {"Dashboard.wasm" }), _ => [] }; _lazyLoadedAssemblies.AddRange(assemblies); }}Dashboard.razor
@page "/dashboard"<PageTitle>Dashboard</PageTitle><h3>Dashboard</h3>It works well if navigate on the links in the navigation menu.
But it does not work, if enter link https://localhost:44302/dashboard directly in the browser
It's possible to resolve, if to add in Program.cs reference to Dashboard project
app.MapRazorComponents<App>() .AddInteractiveWebAssemblyRenderMode() .AddAdditionalAssemblies(typeof(BlazorApp.Client._Imports).Assembly) .AddAdditionalAssemblies(typeof(Dashboard._Imports).Assembly);But in this case the Dashboard page loads 2 times, if to enter it directly in the browser.