Migrating to Blazor Web App
ASP.NET Blazor in .NET 8 allows you to use a single powerful component model to handle all of your web UI needs, including server-side rendering, client-side rendering, streaming rendering, progressive enhancement, and much more!
ABP v8.2.x supports the new Blazor Web App template, in this guide, we will introduce some new changes and features in the new Blazor Web App template.
Create a new Blazor Web App
Please make sure you have installed the 8.2.x version of the ABP CLI.
You can create a new Blazor Web App using the abp new BookStore -t app -u blazor-webapp
command. The -u blazor-webapp
option is used to select the Blazor Web App template.
Of course, you can also create Blazor WASM and Blazor Server applications. We have changed them to use the new Blazor Web App mode:
Render modes
The template project use different render modes for different types of projects in the App.razor
component.
Type | Render mode |
---|---|
WASM | InteractiveWebAssembly(prerender: false) |
Server | InteractiveServer |
WebApp | InteractiveAuto |
The key changes of the new Blazor Web App template
The new Web App template has two projects, each containing a system of ABP modules.
- MyCompanyName.MyProjectName.Blazor.WebApp
- MyCompanyName.MyProjectName.Blazor.WebApp.Client
MyCompanyName.MyProjectName.Blazor.WebApp
The Blazor.WebApp
is the startup project, and there is an App.razor
component in the Blazor.WebApp
project, which is the root component of the Blazor application.
The main differences between it, and a regular Blazor server project are:
- You need to
PreConfigure
theIsBlazorWebApp
totrue
inAbpAspNetCoreComponentsWebOptions
:
- Add related services to the container. Add assembly of
MyProjectNameBlazorClientModule
to theAdditionalAssemblies
by configuringAbpRouterOptions
:
- Add
UseAntiforgery
middleware andMapRazorComponents/AddInteractiveServer/WebAssemblyRenderMode/AddAdditionalAssemblies
in theOnApplicationInitialization
method.
MyCompanyName.MyProjectName.Blazor.WebApp.Client
There is a Routers.razor
component in the Blazor.WebApp.Client
project, which is used by the App.razor
component.
The main differences between it and a regular Blazor WASM project are:
- You need to
PreConfigure
theIsBlazorWebApp
totrue
inAbpAspNetCoreComponentsWebOptions
:
- Use
AddBlazorWebAppServices
to replaceAuthentication
code:
- Remove the
builder.RootComponents.Add<App>("#ApplicationContainer");
code.
MyCompanyName.MyProjectName.Blazor.WebApp.Tiered and MyCompanyName.MyProjectName.Blazor.WebApp.Tiered.Client
The tiered projects are the same as the WebApp projects, but the authentication configuration is different.
We need share the access_token
to Client
project.
Add code block to App.razor
of MyCompanyName.MyProjectName.Blazor.WebApp.Tiered
as below:
Add ConfigureAuthentication
to MyProjectNameBlazorClientModule
of MyCompanyName.MyProjectName.Blazor.WebApp.Tiered.Client
as below:
ABP Bundle
You need set IsBlazorWebApp
and InteractiveAuto
to true
in the appsettings.json
file of the MyCompanyName.MyProjectName.Blazor.WebApp.Client
project:
For Blazor WASM and Blazor Server applications, you need to set IsBlazorWebApp
to true
and not need to change the InteractiveAuto
:
Then run the abp bundle
command to under the MyCompanyName.MyProjectName.Blazor.WebApp.Client
project to generate the global.css
and global.js
files.
Troubleshooting
If you encounter any problems during the migration, please create a new template project and compare the differences between the new and old projects.