Introduction
This is part 2 of the article. It focuses on consuming the .NET Core API created in part 1, and binding the data in our Blazor app. By the end of the article, we will have a fully functional Blazor site with Add/Edit and Delete features.
Prerequisites
This article assumes you have a basic working knowledge of Blazor web assembly and .NET Core.
Read my previous article on Blazor Web Assembly 3.2 Add/Edit/Delete Fully Functional Application-Part 1
We will be consuming the .NET Core API created in the first part to bind in our Blazor client app.
Through this article, we will cover the following topics:
- Create and register data service in Blazor to consume a .NET Core API.
- Creating Blazor razor pages and data binding.
- Creating a custom Blazor component for Add Employee.
Output
Employee Home Page

Employee Detail Page

Implementation
Step 1 - Create and register data service in Blazor to consume .NET Core API
Create a new folder service in EmployeePortal.Client and add IEmployeeDataService.
This will contain a definition of all the methods to be used in EmployeeDataService.
IEmployeeDataService
- public interface IEmployeeDataService {
- Task < IEnumerable < Employee >> GetAllEmployees();
- Task < Employee > AddEmployee(Employee employee);
- Task < Employee > GetEmployeeDetails(int employeeId);
- Task UpdateEmployee(Employee employee);
- Task DeleteEmployee(int employeeId);
- }
EmployeeDataService
- public class EmployeeDataService: IEmployeeDataService {
- private readonly HttpClient _httpClient;
- public EmployeeDataService(HttpClient httpClient) {
- _httpClient = httpClient;
- }
- public async Task < IEnumerable < Employee >> GetAllEmployees() {
- return await JsonSerializer.DeserializeAsync < IEnumerable < Employee >> (await _httpClient.GetStreamAsync($ "api/employee"), new JsonSerializerOptions() {
- PropertyNameCaseInsensitive = true
- });
- }
- public async Task < Employee > AddEmployee(Employee employee) {
- var employeeJson = new StringContent(JsonSerializer.Serialize(employee), Encoding.UTF8, "application/json");
- var response = await _httpClient.PostAsync($ "api/employee", employeeJson);
- if (response.IsSuccessStatusCode) {
- return await JsonSerializer.DeserializeAsync < Employee > (await response.Content.ReadAsStreamAsync());
- }
- return null;
- }
- public async Task < Employee > GetEmployeeDetails(int employeeId) {
- return await JsonSerializer.DeserializeAsync < Employee > (await _httpClient.GetStreamAsync($ "api/employee/{employeeId}"), new JsonSerializerOptions() {
- PropertyNameCaseInsensitive = true
- });
- }
- public async Task UpdateEmployee(Employee employee) {
- var employeeJson = new StringContent(JsonSerializer.Serialize(employee), Encoding.UTF8, "application/json");
- await _httpClient.PutAsync("api/employee", employeeJson);
- }
- public async Task DeleteEmployee(int employeeId) {
- await _httpClient.DeleteAsync($ "api/employee/{employeeId}");
- }
- }
Register EmployeeDataService and IEmployeeDataService in program.cs of cllient app
builder.Services.AddHttpClient<IEmployeeDataService, EmployeeDataService>(client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress));
Step 2 - Creating Blazor razor pages and data binding
Add a new Razor component and EmployeePage class inside the pages folder.

EmployeePage.cs
- public partial class EmployeePage: ComponentBase {
- public IEnumerable < EmployeePortal.Shared.Employee > Employees {
- get;
- set;
- }
- [Inject]
- public IEmployeeDataService EmployeeDataService {
- get;
- set;
- }
- public AddEmployeeDialog AddEmployeeDialog {
- get;
- set;
- }
- protected async override Task OnInitializedAsync() {
- Employees = (await EmployeeDataService.GetAllEmployees()).ToList();
- }
- protected void QuickAddEmployee() {
- AddEmployeeDialog.Show();
- }
- public async void AddEmployeeDialog_OnDialogClose() {
- Employees = (await EmployeeDataService.GetAllEmployees()).ToList();
- StateHasChanged();
- }
- }
Employee page class will inherit from the Component base.
OnInitializedAsync will be called when the componenet is been Initialized and we would call GetAllEmployees at this point in time.
IEmployeeDataService needs to be injected to call the GetAllEmployees method.
Employeerazor
- @page "/"
- @using EmployeePortal.Client.Components;
- <h1 class="page-title">All employees</h1>
- @if (Employees == null)
- {
- <p>
- <em>Loading...</em>
- </p>
- }
- else
- {
- <table class="table">
- <thead>
- <tr>
- <th>Employee ID</th>
- <th>First name</th>
- <th>Last name</th>
- <th></th>
- </tr>
- </thead>
- <tbody>
- @foreach (var employee in Employees)
- {
- <tr>
- <td>@employee.EmployeeId</td>
- <td>@employee.FirstName</td>
- <td>@employee.LastName</td>
- <td>
- <a href="@($"detail/{employee.EmployeeId}")" class="btn btn-primary-details table-btn">
- <i class="fas fa-info-circle"></i>
- Details
- </a>
- <a Edit href="@($"edit/{employee.EmployeeId}")" class="btn btn-primary-edit table-btn">
- <i class="fas fa-edit"></i>
- Edit
- </a>
- </td>
- </tr>
- }
- </tbody>
- </table>
- }
- <button @onclick="QuickAddEmployee" class="btn btn-dark table-btn quick-add-btn"> + </button>
- <AddEmployeeDialog @ref="AddEmployeeDialog" CloseEventCallback="@AddEmployeeDialog_OnDialogClose"></AddEmployeeDialog>

Fahad MirzaPosted Aug 5, 2024, 8:57 AM
Part 1 is released on 26 Jul 2024 and this Part 2 is released on Aug 21, 2020. How did this time travel take place???
Alan WeiPosted Jul 21, 2022, 4:00 PM
Where is the code forAddEmployeeDialog, I don't see it in part 1 or part 2, thanks
Mario BockPosted Apr 20, 2021, 10:31 AM
I am looking for a solution for Blazor Webassembly with connection to an MS-SQL database 2019. I use storage procedures. This Blazor Web Assembly 3.2 article looked promising. Part 1 is great, with Part 2 I have considerable problems. Can you get the complete code as a project? Thank you for your answer..
Nathanael MattayPosted Feb 25, 2021, 4:54 PM
If I am going to have many data classes, will I have to have a separate data service and repository for every single class? Or is there a way to have a generic data service that has the same CRUD actions with a generic variable that can be used for various classes?
ian ebdaoPosted Jan 17, 2021, 8:53 AM
Im stuck in AddEmployeeDialog i can't find the code
Prasad RanePosted Oct 29, 2020, 9:17 AM
Can u copy paste part of your code here.I can have a check
Daniela SantamariaPosted Oct 28, 2020, 5:20 PM
Hi, i was wondering if you could help me. I get an CS1056 Unexpected character '$' on the EmployeeDataService, i don't know why and I can't fix it