Set up Blazor hosting in the AppHost

Цей контент ще не доступний вашою мовою.

Blazor logo

This article is the reference for the Aspire Blazor hosting integration. It lists the AppHost APIs — with examples for both AppHost.cs and apphost.mts — that you use to model Blazor WebAssembly projects and a Blazor Gateway in your AppHost project.

If you’re new to this integration, start with Get started with Blazor hosting. For how apps connect through the gateway and service discovery properties, see Connect Blazor apps and APIs.

To start building an Aspire app that uses Blazor hosting, install the 📦 Aspire.Hosting.Blazor NuGet package:

Terminal
aspire add Aspire.Hosting.Blazor

Learn more about aspire add in the command reference.

This updates your aspire.config.json with the Blazor hosting package:

aspire.config.json
{
"packages": {
"Aspire.Hosting.Blazor": "13.5.3"
}
}

Use AddBlazorWasmProject / addBlazorWasmProject to add a Blazor WebAssembly project to the AppHost model.

apphost.mts
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const api = await builder.addProject('api', '../MyApi/MyApi.csproj');
const blazorApp = await builder
.addBlazorWasmProject('app', '../MyBlazorApp/MyBlazorApp.csproj')
.withReference(await api.getEndpoint('http'));
await builder.build().run();

Use AddBlazorGateway / addBlazorGateway to add the Blazor Gateway resource. The gateway is the browser-facing endpoint for your hosted Blazor app.

apphost.mts
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const gateway = await builder
.addBlazorGateway('gateway')
.withExternalHttpEndpoints();
await builder.build().run();

Use WithBlazorClientApp / withBlazorClientApp to bind the WebAssembly project resource to the Blazor Gateway:

apphost.mts
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const weatherApi = await builder.addProject(
'weatherapi',
'../WeatherApi/WeatherApi.csproj'
);
const blazorApp = await builder
.addBlazorWasmProject('app', '../MyBlazorApp/MyBlazorApp.csproj')
.withReference(await weatherApi.getEndpoint('http'));
await builder
.addBlazorGateway('gateway')
.withExternalHttpEndpoints()
.withBlazorClientApp(blazorApp);
await builder.build().run();