Перейти к содержимому
Docs Try Aspire
Docs Try

Connect Blazor apps and APIs

Это содержимое пока не доступно на вашем языке.

Blazor logo

This page describes how to connect Blazor app resources and APIs after you’ve already modeled them in your AppHost. For the full hosting API surface, see Set up Blazor hosting in the AppHost.

When you use WithReference / withReference, Aspire publishes service discovery endpoint values as environment variables. The key format is:

  • services__{resource-name}__{endpoint-name}__{index}

For example, if you reference an API resource named weatherapi with an http endpoint, Aspire exposes:

  • services__weatherapi__http__0

Common properties used in Blazor-hosted app models are:

PropertyDescription
services__{resource}__http__0The first HTTP endpoint URL for a referenced resource
services__{resource}__https__0The first HTTPS endpoint URL for a referenced resource
services__{resource}__{endpoint}__0A named endpoint URL when the endpoint name is custom

Use the AppHost to connect APIs to a Blazor WebAssembly project resource, then associate the client app with a Blazor Gateway.

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

Any app resource that references the same APIs can consume the services__... values directly.

C# — Program.cs
var weatherApiUrl = Environment.GetEnvironmentVariable("services__weatherapi__http__0");