Watch Aspire live streamsDocsTry Aspire

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": "17.0.0"
}
}

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.

apphost.mts
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
();

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.

apphost.mts
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
();

WithExternalHttpEndpointswithExternalHttpEndpointsWithExternalHttpEndpoints marks the gateway endpoint as externally accessible so browser traffic can reach your app.

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:

apphost.mts
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
();

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.