🎓 All Courses | 📚 Blazor WASM Syllabus
Stickipedia University
📋 Study this course on TaskLoco

Blazor WASM apps communicate with backend APIs using HttpClient. The System.Net.Http.Json extension methods make JSON calls concise and type-safe.

Registering HttpClient

// Program.cs
builder.Services.AddScoped(sp =>
    new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

GET Request

@inject HttpClient Http

protected override async Task OnInitializedAsync()
{
    products = await Http.GetFromJsonAsync<List<Product>>("/api/products");
}

POST Request

var response = await Http.PostAsJsonAsync("/api/products", newProduct);
if (response.IsSuccessStatusCode)
{
    var created = await response.Content.ReadFromJsonAsync<Product>();
}

PUT and DELETE

await Http.PutAsJsonAsync($"/api/products/{id}", updated);
await Http.DeleteAsync($"/api/products/{id}");

Error Handling

try
{
    data = await Http.GetFromJsonAsync<List<Item>>("/api/items") ?? new();
}
catch (HttpRequestException ex)
{
    errorMessage = ex.Message;
}

YouTube • Top 10
Blazor WASM: HttpClient and Calling APIs
Tap to Watch ›
📸
Google Images • Top 10
Blazor WASM: HttpClient and Calling APIs
Tap to View ›

Reference:

Wikipedia: REST

image for linkhttps://en.wikipedia.org/wiki/Representational_state_transfer

📚 Blazor WASM — Full Course Syllabus
📋 Study this course on TaskLoco

TaskLoco™ — The Sticky Note GOAT