
Blazor WASM apps communicate with backend APIs using HttpClient. The System.Net.Http.Json extension methods make JSON calls concise and type-safe.
// Program.cs
builder.Services.AddScoped(sp =>
new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });@inject HttpClient Http
protected override async Task OnInitializedAsync()
{
products = await Http.GetFromJsonAsync<List<Product>>("/api/products");
}var response = await Http.PostAsJsonAsync("/api/products", newProduct);
if (response.IsSuccessStatusCode)
{
var created = await response.Content.ReadFromJsonAsync<Product>();
}await Http.PutAsJsonAsync($"/api/products/{id}", updated);
await Http.DeleteAsync($"/api/products/{id}");try
{
data = await Http.GetFromJsonAsync<List<Item>>("/api/items") ?? new();
}
catch (HttpRequestException ex)
{
errorMessage = ex.Message;
}Reference:
TaskLoco™ — The Sticky Note GOAT