C# / .NET Compendium PLEN
.NET / C# / Web API, Blazor

Blazor - why it is NOT MVC

This is a key point to make in an interview, because wrongly assigning Blazor to MVC is a sign of superficial knowledge. Blazor is a component framework (philosophically similar to React or Angular), not MVC.

Two Blazor hosting models

Blazor Server - the logic runs on the server, and the UI is updated through SignalR (WebSocket) in real time.

Blazor WebAssembly (WASM) - all the C# code is compiled to WebAssembly and executed in the browser, without round trips to the server on every interaction.

A Blazor component - the equivalent of "View + some logic"

@page "/orders/{id:int}"
@inject IOrderService OrderService

<h3>Order #@Order?.Id</h3>

@if (Order == null)
{
    <p>Loading...</p>
}
else
{
    <p>Klient: @Order.CustomerName</p>
    <p>Total: @Order.Total PLN</p>
    <button @onclick="ShipOrder">Ship the order</button>
}

@code {
    [Parameter] public int Id { get; set; }
    private OrderDto Order;

    protected override async Task OnInitializedAsync()
    {
        Order = await OrderService.GetOrderAsync(Id);
    }

    private async Task ShipOrder()
    {
        await OrderService.ShipOrderAsync(Id);
        Order = await OrderService.GetOrderAsync(Id);
    }
}

Why it is closer to MVVM than MVC

A Blazor component combines what in MVC was split into a Controller + View into one unit:

MVC Blazor MVVM (WPF)
UI (markup) View (.cshtml) The HTML section in .razor XAML
Presentation logic Controller The @code section in the same file ViewModel (a separate file)
UI refresh A full page reload (or manual JS) Automatic re-rendering when the state changes Automatic through INotifyPropertyChanged/binding
DI Through the controller's constructor @inject Through the ViewModel's constructor

@inject in Blazor is DI - exactly the same Microsoft.Extensions.DependencyInjection container:

// Program.cs
builder.Services.AddScoped<IOrderService, OrderService>();
@inject IOrderService OrderService

Component lifecycle

protected override void OnInitialized() { }           // odpowiednik konstruktora ViewModelu
protected override async Task OnInitializedAsync() { } // dla async inicjalizacji
protected override void OnParametersSet() { }           // when the [Parameter]s change
protected override bool ShouldRender() => true;          // controls whether to re-render

StateHasChanged - manually refreshing the UI

private async Task LoadDataInBackground()
{
    var data = await _service.GetDataAsync();
    Orders = data;
    StateHasChanged(); // tell Blazor to re-render the component
}

This is conceptually very close to what PropertyChanged does in WPF.

Blazor Server vs WASM - practical differences

Blazor Server Blazor WebAssembly
Where the code runs The server The browser (WASM)
Communication SignalR (a persistent WebSocket connection) Directly in the browser, HTTP to the API when needed
Interaction latency Depends on the network Immediate (the code is local)
Download size Small Large (downloads the whole .NET runtime)
Offline ❌ No ✅ Can work offline (PWA)
Server scaling Every user = persistent state on the server The server only serves static files + the API

Where Web API and Blazor meet in the architecture

Blazor WASM (frontend) → HttpClient → ASP.NET Core Web API (backend) → Service → EF Core → baza
@inject HttpClient Http

@code {
    private List<OrderDto> orders;
    protected override async Task OnInitializedAsync()
    {
        orders = await Http.GetFromJsonAsync<List<OrderDto>>("api/orders");
    }
}

Blazor Server, on the other hand, doesn't need a separate Web API in the middle - a component can inject IOrderService directly through DI and call it locally (because the code runs on the server anyway).

Buy me a coffee