REST API Integration
A Blazor client and an ASP.NET Core API make a clean full-stack pair — especially when they share their models.
On this page · 5 min · Intermediate
On this page
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Intermediate
- Updated
- Oct 29, 2026
Related tutorials
Interview questions
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#
// 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}");
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#
- The shared record defines the contract once.
- The API pages in SQL with Skip/Take and returns the total.
- The client deserialises straight into the shared type.
- 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#
Exposes internal fields and causes cycles. Return DTOs.
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
var query = db.Products.AsNoTracking()
.Where(p => q == null || p.Name.Contains(q))
.OrderBy(p => p.Name);
Mini quiz#
Why share DTOs between Blazor client and API?
Want more? Practise all Blazor MCQs · Blazor interview questions
My notes#
Saved in this browser.