Blazor · Data

REST API Integration

A Blazor client and an ASP.NET Core API make a clean full-stack pair — especially when they share their models.

Intermediate 5 min Lesson 19 of 31Examples target .NET 9 Blazor Web App.
0/31 in Blazor
On this page · 5 min · Intermediate

Learning objectives#

  • Share DTOs between client and API
  • Configure CORS for a separate client
  • Consume paged results
  • Read ProblemDetails validation errors

Explanation#

Put DTOs in a shared class library referenced by both projects, so a renamed property breaks the build instead of production.

If the client is hosted on a different origin, the API must allow it with CORS.

Return paged results (items + total count) instead of whole tables.

ASP.NET Core returns validation errors as ProblemDetails JSON; read it to show field messages in the UI.

Code example: Paged products from the API#

C#Shared/PagedResult.cs + client call Illustrative
// Shared project
public record PagedResult<T>(IReadOnlyList<T> Items, int Total, int Page, int PageSize)
{
    public int TotalPages => (int)Math.Ceiling(Total / (double)PageSize);
}


// API endpoint
app.MapGet("/api/products", async (ShopDb db, int page = 1, int pageSize = 20) =>
{
    var query = db.Products.AsNoTracking().OrderBy(p => p.Name);
    var items = await query.Skip((page - 1) * pageSize).Take(pageSize)
        .Select(p => new ProductDto(p.Id, p.Name, p.Price)).ToListAsync();
    return new PagedResult<ProductDto>(items, await query.CountAsync(), page, pageSize);
});


// Blazor client
var result = await http.GetFromJsonAsync<PagedResult<ProductDto>>(
quot;api/products?page=
{page}");
Expected output
The client receives 20 products plus Total = 412, so it can render “Page 1 of 21” with next/previous buttons.

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. The shared record defines the contract once.
  2. The API pages in SQL with Skip/Take and returns the total.
  3. The client deserialises straight into the shared type.
  4. TotalPages drives the pager UI.

Real-world use case#

The Employee Management project: a Blazor or React front end over one ASP.NET Core Web API.

Common mistakes#

Returning EF entities from the API

Exposes internal fields and causes cycles. Return DTOs.

Allowing any origin with credentials

Allow only your client’s origin.

Best practices#

  • Shared DTO library
  • Page and filter on the server
  • Return ProblemDetails for errors
  • Version the API when contracts change

Remember this#

Share DTOs, not entities.

Items + Total = paging.

Different origin → CORS on the API.

Practice exercise#

Your task: Add a search parameter q to the API endpoint.

Show solution
C# Illustrative
var query = db.Products.AsNoTracking()
    .Where(p => q == null || p.Name.Contains(q))
    .OrderBy(p => p.Name);

Mini quiz#

REST API Integration quizMultiple choice Question 1 of 1

Why share DTOs between Blazor client and API?

Score so far: 0/0

Want more? Practise all Blazor MCQs · Blazor interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.