Lewati ke konten
Docs Try Aspire
Docs Try

Set up Blazor hosting in the AppHost

Konten ini belum tersedia dalam bahasa Anda.

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.

Or, choose a manual installation approach:

C# — AppHost.cs
#:package Aspire.Hosting.Blazor@*
XML — AppHost.csproj
<PackageReference Include="Aspire.Hosting.Blazor" Version="*" />

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

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var api = builder.AddProject<Projects.MyApi>("api");
var blazorApp = builder.AddBlazorWasmProject("app", "../MyBlazorApp/MyBlazorApp.csproj");
blazorApp.WithReference(api);
builder.Build().Run();

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

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var gateway = builder.AddBlazorGateway("gateway")
.WithExternalHttpEndpoints();
builder.Build().Run();

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

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var weatherApi = builder.AddProject<Projects.WeatherApi>("weatherapi");
var blazorApp = builder.AddBlazorWasmProject("app", "../MyBlazorApp/MyBlazorApp.csproj");
blazorApp.WithReference(weatherApi);
var gateway = builder.AddBlazorGateway("gateway")
.WithExternalHttpEndpoints();
gateway.WithBlazorClientApp(blazorApp);
builder.Build().Run();