Goal : Move Blazor components like Account
pages to a separate project in order to have a clear separation of features in different modules.
- Create a new Blazor app
ModularBlazor.App
with Authentication. - Execute
dotnet ef update database
to create the database. - Create a new user using the
Register
page and login to test authenticated pages.
- Create project
ModularBlazor.Account
in Modules folder. - Add reference of
ModularBlazor.Account
toModularBlazor.App
application. - Change Project Sdk to
<Project Sdk="Microsoft.NET.Sdk.Razor">
- Add AspNetCore assemblies references to
ModularBlazor.Account.csproj
:
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Components" Version="9.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="9.0.1" />
- Move
Component\Account
Pages toModularBlazor.Account
project. - Rename namespaces to
ModularBlazor.Account.Components.Account
. - Add
_Imports.razor
file toModularBlazor.Account.Components
folder - Change scope of
internal sealed
classes topublic
to use them in the main application.
- Add
Routes.razor.cs
belowRoutes.razor
, declare staticAdditionalAssemblies
and add theModularBlazor.Account
Assembly:
public partial class Routes
{
public static List<Assembly> AdditionalAssemblies = new List<Assembly>()
{
typeof(ModularBlazor.Account.Components._Imports).Assembly
};
}
- In
Program.cs
, call theAddAdditionalAssemblies()
method in order to load the module assemblies in the main application:
App.MapRazorComponents<App>()
.AddInteractiveServerRenderMode()
.AddAdditionalAssemblies(Routes.AdditionalAssemblies.ToArray());
- Move
Components\Layout
folder toModularBlazor.Account
project (ManageLayout
needs the main Layout). - Move
Data
folder toModularBlazor.Account
project (ApplicationDbContext
,ApplicationUser
, ...).
- Create project
ModularBlazor.Data
- Add Entity Framework Core packages to
ModularBlazor.Data.csproj
. - Move
Data
folder toModularBlazor.Data
. - Add reference of
ModularBlazor.Data
toModularBlazor.Account
project. - Change namespace
ModularBlazor.App.Data
toModularBlazor.Data
.
- Create project
ModularBlazor.Layout
- Move
Layout
folder toModularBlazor.Layout
project. - Add reference of
ModularBlazor.Layout
toModularBlazor.Account
project and toModularBlazor.App
application. - Change namespaces to use
ModularBlazor.Layout.Components.Layout
where needed (Routes
,Account\Shared\ManageLayout
).
Inspired by BlazorAdmin