BlazorExtensionsAspire 94.2025.817.1850

dotnet add package BlazorExtensionsAspire --version 94.2025.817.1850
                    
NuGet\Install-Package BlazorExtensionsAspire -Version 94.2025.817.1850
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BlazorExtensionsAspire" Version="94.2025.817.1850" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BlazorExtensionsAspire" Version="94.2025.817.1850" />
                    
Directory.Packages.props
<PackageReference Include="BlazorExtensionsAspire" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add BlazorExtensionsAspire --version 94.2025.817.1850
                    
#r "nuget: BlazorExtensionsAspire, 94.2025.817.1850"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package BlazorExtensionsAspire@94.2025.817.1850
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=BlazorExtensionsAspire&version=94.2025.817.1850
                    
Install as a Cake Addin
#tool nuget:?package=BlazorExtensionsAspire&version=94.2025.817.1850
                    
Install as a Cake Tool

BlazorExtensionsAspire

Extension methods and helpers for integrating Blazor WebAssembly projects with .NET Aspire distributed applications.

Features

  • Blazor WebAssembly project environment integration
  • Automatic appsettings.json endpoint injection for API URLs
  • Utility methods for Aspire project resource management
  • Simplifies connecting Blazor WebAssembly frontends to backend APIs in Aspire distributed apps

Installation

Install via NuGet:

dotnet add package BlazorExtensionsAspire

Usage

1. Registering a Blazor WebAssembly Project in Aspire

In your Aspire AppHost project, use the extension method to register your Blazor WebAssembly project and link it to your API service:

using Blazor.Extension;

var builder = DistributedApplication.CreateBuilder(args);

var apiService = builder.AddProject<Projects.BlazorExtensions_ApiService>("apiservice")
    .WithHttpHealthCheck("/health");

builder.AddWebAssemblyProject<Projects.BlazorWebAssProject>("webfrontend", apiService)
    .WithReference(apiService)
    .WaitFor(apiService);

builder.Build().Run();

This will automatically inject the API service endpoint into the Blazor WebAssembly project's wwwroot/appsettings.json and set the environment variable apiservice_host.

2. Consuming the API URL in Blazor WebAssembly

In your Blazor WebAssembly project, you can retrieve the API URL from configuration and register it for HTTP clients:

var hostApi = builder.Configuration["apiservice_host"];
if (string.IsNullOrEmpty(hostApi))
{
    hostApi = builder.HostEnvironment.BaseAddress;
    if (!hostApi.EndsWith("/"))
    {
        hostApi += "/";
    }
}

builder.Services.AddKeyedScoped("api", (sp, _) => new HttpClient { BaseAddress = new Uri(hostApi) });
builder.Services.AddKeyedScoped("local_host", (sp, _) => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

You can then inject the correct HttpClient in your components using the [Inject(Key = "api")] attribute to call your backend API.

Example Razor Component

<PageTitle>Home</PageTitle>
 
<h1>API</h1>
@httpClientAPI?.BaseAddress

<h1>Local Host</h1>
@httpClientLocal?.BaseAddress
 
@code {
    [Inject(Key = "local_host")]
    public HttpClient? httpClientLocal { get; set; }

    [Inject(Key = "api")]
    public HttpClient? httpClientAPI { get; set; }
}


How it works

  • The AddWebAssemblyProject extension method links your Blazor WebAssembly frontend to a backend API resource.
  • It writes the discovered API endpoint to wwwroot/appsettings.json and sets an environment variable (e.g., apiservice_host).
  • Your Blazor WebAssembly app reads this value at startup and configures its HttpClient accordingly.
  • Also, do not forget to add CORS support in your API service to allow requests from the Blazor WebAssembly frontend. ( when deploying to production, ensure proper CORS policies are set up for security or use deploy in the same site to avoid CORS ! )

License

See LICENSE for details.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
94.2025.817.1850 138 8/17/2025
94.2025.817.1650 103 8/17/2025

Initial release.