Blazor WASM Application


This sample Blazor WASM (WebAssembly application, aims to demonstrate a client-side application.
The sample use case is an application that lists all the Noble Houses in a dropdown. The user can then select any of the House to retrieve information around the House.
For use case, a simple HTTP REST API has been created which serves as a data provider to the client application.

Blazor WASM Application
Blazor WASM Application
Blazor WASM Application
Blazor WASM Application
Blazor WASM Application


The MainLayout aims to set up a structure for the application. Here, we can add the Header, Navigation Bar, and Footer component place holders.
  1. @inherits LayoutComponentBase  
  2.     <div class="w3-container">  
  3.             @Body  
  4.     </div>  
  6. @code{  

It displays the list of Houses in a dropdown. The value that is selected by the user is bind to the _selectedHouse variable.
The foreach loops over the Houses collection & adds an option to the select input.
The button Get Details, when clicked an event is generated which is handled by HandleGet function.
  1. <div class="w3-row-padding">  
  2.     <div class="w3-col m3">  
  3.         <select class="w3-select w3-border" id="houses" @bind="_selectedHouse">  
  4.             <option>---Select House---</option>  
  6.             @foreach (var house in Houses)  
  7.             {  
  8.                 <option value="@house.Id">@house.Name</option>  
  9.             }  
  11.         </select>  
  12.     </div>  
  13.     <div class="w3-col m1 w3-left ">  
  14.         <button class="w3-button w3-black w3-round-xxlarge" @onclick="HandleGet">Get Details</button>  
  15.     </div>  
  16. </div>  
The below code is executed on the pageload or an Initialized event. Here, HttpClient is used to make an HTTP call to the API.
  1. protected override async Task OnInitializedAsync()  
  2.     {  
  3.         Houses = await HttpClient.GetFromJsonAsync<House[]>("house");  
  4.     }  
The HandleGet is the event handler that handles the click event of the Get Details button. Here we retrieve the selected value by the user which is bound to _selectedHouse.
The selected value is then used to make an HTTP call to retrieve the House details.
  1. private async Task HandleGet()  
  2.    {  
  3.        long houseid;  
  5.        if (long.TryParse(_selectedHouse, out houseid) == false)  
  6.        {  
  7.            return;  
  8.        }  
  10.        House = await HttpClient.GetFromJsonAsync<House>($"house/{houseid}");  
  11.    } 
Once the House details are retrieved and assigned to the variable, the below code helps to display the information.
  1. @if (House != null)  
  2. {  
  3.     <div class="w3-card-4">  
  5.         <header class="w3-container">  
  6.             <h1>@House.Name</h1>  
  7.         </header>  
  9.         <div class="w3-container">  
  10.             <div class="w3-row-padding">  
  11.                 <div class="w3-col m2 ">  
  12.                     <img src="data:image/jpeg;base64,@House.Banner" style="max-width:100%;padding:10px;" alt="Banner" />  
  13.                 </div>  
  14.                 <div class="w3-col m10" style="padding:10px;">  
  15.                     @House.Information  
  16.                 </div>  
  17.             </div>  
  18.         </div>  
  20.     </div>  
  21. }  

HTTP Client Injection

HTTP Client is configured & added to the Service collection so that it can be injected throughout the application. For the current purpose, the base URL is set to point towards the API which provides data for the application.
  1. builder.Services.AddTransient(sp => new HttpClient  
  2.            {  
  3.                BaseAddress = new Uri("http://localhost:5010/api/")  
  4.            }); 
Injecting the HTTP Client in pages.
  1. @inject HttpClient HttpClient 
Running/Developing the Application.
Execute the http://localhost:5010/api/seed endpoint on the API URL, so that an In-Memory database can be seeded with information.
  1. dotnet run Basics.GameOfThrones.Api.csproj  
Note the watch switch. The watch monitors any changes made to any files, once the changes are saved, it automatically recompiles the application.
  1. dotnet watch run Basics.GameOfThrones.BlazorWebApp.csproj      
I hope that this article helps you to understand a quick sample application in Blazor.
The source code is available on GitHub

Similar Articles