Set up Blazor hosting in the AppHost
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.
Installation
Section titled “Installation”To start building an Aspire app that uses Blazor hosting, install the 📦 Aspire.Hosting.Blazor NuGet package:
aspire add Aspire.Hosting.BlazorLearn more about aspire add in the
command reference.
This updates your aspire.config.json with the Blazor hosting package:
{ "packages": { "Aspire.Hosting.Blazor": "17.0.0" }}aspire add Aspire.Hosting.BlazorLearn more about aspire add in the
command reference.
Or, choose a manual installation approach:
#:package Aspire.Hosting.Blazor@*<PackageReference Include="Aspire.Hosting.Blazor" Version="*" />Add a Blazor WebAssembly app
Section titled “Add a Blazor WebAssembly app”Use AddBlazorWasmAppaddBlazorWasmProjectAddBlazorWasmProject to add a Blazor WebAssembly project to the AppHost model. In TypeScript AppHosts, this API is named addBlazorWasmProject. Aspire doesn’t launch the WebAssembly app as its own process; a Blazor Gateway serves it to the browser.
import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.aspire/modules/aspire.mjs';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const api: DotnetProjectResource
api = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addDotnetProject(name: string, path: string, options?: DotnetProjectOptions): DotnetProjectResource
Adds a .NET application resource.
addDotnetProject('api', '../MyApi/MyApi.csproj');
const const blazorApp: BlazorWasmAppResource
blazorApp = await const builder: IDistributedApplicationBuilder
builder .IDistributedApplicationBuilder.addBlazorWasmProject(name: string, projectPath: string): BlazorWasmAppResource
Registers a Blazor WebAssembly project as a resource without launching it as a process. Prefer AddBlazorWasmProject which uses IProjectMetadata for path discovery.
addBlazorWasmProject('app', '../MyBlazorApp/MyBlazorApp.csproj') .IResourceWithEnvironment.withReference(source: IResource | EndpointReference | string | uri, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): BlazorWasmAppResource (+1 overload)
Adds a reference to another resource
withReference(const api: DotnetProjectResource
api);
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.build(): DistributedApplication
Builds the distributed application
build().DistributedApplication.run(cancellationToken?: cancellationToken): void
Runs the distributed application
run();#pragma warning disable ASPIREBLAZOR001
var builder = DistributedApplication.CreateBuilder(args);
var api = builder.AddDotnetProject("api", "../MyApi/MyApi.csproj");
var blazorApp = builder.AddBlazorWasmApp("app", "../MyBlazorApp/MyBlazorApp.csproj") .WithReference(api);
builder.Build().Run();Add a Blazor Gateway
Section titled “Add a Blazor Gateway”Use AddDotnetProjectBlazorGatewayaddDotnetProjectBlazorGatewayAddDotnetProjectBlazorGateway to add the Blazor Gateway resource. The gateway is the browser-facing endpoint for your Blazor app: it serves the WebAssembly client and forwards the client’s API and telemetry traffic. Aspire provides the gateway, so you don’t need to create a gateway project.
import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.aspire/modules/aspire.mjs';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const gateway: DotnetProjectResource
gateway = await const builder: IDistributedApplicationBuilder
builder .IDistributedApplicationBuilder.addDotnetProjectBlazorGateway(name: string): DotnetProjectResource
Adds the built-in Blazor gateway as a .NET program resource.
addDotnetProjectBlazorGateway('gateway') .ExecutableResource.withExternalHttpEndpoints(): DotnetProjectResource
Marks existing http or https endpoints on a resource as external.
withExternalHttpEndpoints();
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.build(): DistributedApplication
Builds the distributed application
build().DistributedApplication.run(cancellationToken?: cancellationToken): void
Runs the distributed application
run();var builder = DistributedApplication.CreateBuilder(args);
var gateway = builder.AddDotnetProjectBlazorGateway("gateway") .WithExternalHttpEndpoints();
builder.Build().Run();WithExternalHttpEndpointswithExternalHttpEndpointsWithExternalHttpEndpoints marks the gateway endpoint as externally accessible so browser traffic can reach your app.
Associate a client app with the gateway
Section titled “Associate a client app with the gateway”Use WithBlazorClientApp(IResourceBuilder<BlazorWasmAppResource> wasmApp, string apiPrefix, string otlpPrefix, bool proxyTelemetry)withBlazorClientApp(wasmApp: BlazorWasmAppResource, apiPrefix?: string, otlpPrefix?: string, proxyTelemetry?: boolean)WithBlazorClientApp to bind the WebAssembly app resource to the Blazor Gateway:
import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.aspire/modules/aspire.mjs';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const weatherApi: DotnetProjectResource
weatherApi = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addDotnetProject(name: string, path: string, options?: DotnetProjectOptions): DotnetProjectResource
Adds a .NET application resource.
addDotnetProject( 'weatherapi', '../WeatherApi/WeatherApi.csproj');
const const blazorApp: BlazorWasmAppResource
blazorApp = await const builder: IDistributedApplicationBuilder
builder .IDistributedApplicationBuilder.addBlazorWasmProject(name: string, projectPath: string): BlazorWasmAppResource
Registers a Blazor WebAssembly project as a resource without launching it as a process. Prefer AddBlazorWasmProject which uses IProjectMetadata for path discovery.
addBlazorWasmProject('app', '../MyBlazorApp/MyBlazorApp.csproj') .IResourceWithEnvironment.withReference(source: IResource | EndpointReference | string | uri, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): BlazorWasmAppResource (+1 overload)
Adds a reference to another resource
withReference(const weatherApi: DotnetProjectResource
weatherApi);
await const builder: IDistributedApplicationBuilder
builder .IDistributedApplicationBuilder.addDotnetProjectBlazorGateway(name: string): DotnetProjectResource
Adds the built-in Blazor gateway as a .NET program resource.
addDotnetProjectBlazorGateway('gateway') .ExecutableResource.withExternalHttpEndpoints(): DotnetProjectResource
Marks existing http or https endpoints on a resource as external.
withExternalHttpEndpoints() .DotnetProjectResource.withBlazorClientApp(wasmApp: BlazorWasmAppResource, options?: { apiPrefix?: string; otlpPrefix?: string; proxyTelemetry?: boolean;} | undefined): DotnetProjectResource (+1 overload)
Attaches a Blazor WebAssembly app to the gateway. The app's resource name becomes its URL path prefix, and its service references are forwarded to the gateway for proxying.
withBlazorClientApp(const blazorApp: BlazorWasmAppResource
blazorApp);
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.build(): DistributedApplication
Builds the distributed application
build().DistributedApplication.run(cancellationToken?: cancellationToken): void
Runs the distributed application
run();#pragma warning disable ASPIREBLAZOR001
var builder = DistributedApplication.CreateBuilder(args);
var weatherApi = builder.AddDotnetProject("weatherapi", "../WeatherApi/WeatherApi.csproj");
var blazorApp = builder.AddBlazorWasmApp("app", "../MyBlazorApp/MyBlazorApp.csproj") .WithReference(weatherApi);
builder.AddDotnetProjectBlazorGateway("gateway") .WithExternalHttpEndpoints() .WithBlazorClientApp(blazorApp);
builder.Build().Run();The gateway forwards the client’s service references to the browser app and is included when you publish the AppHost. Browser-debugger resources are created only for local runs.