Exploring Blazor Web Assembly App

Introduction

 
Blazor App has two hosting models: Server and Web Assembly.
 
Blazor Web Assembly Hosting Model for applications is used for running Blazor apps on client side browsers. The app is executed on the Web browser. All the dependencies are downloaded in the browser and the execution of the app takes place. App assets are deployed as static content and served to the client.
 
Blazor.webassembly.js file handles downloading .NET dependencies and running app runtime.
 
Exploring Blazor Web Assembly App
[Source: https://docs.microsoft.com/en-gb/aspnet/core/blazor/hosting-models?view=aspnetcore-3.1#blazor-webassembly]
 
Getting Started:
 
Software Requirements
  • Visual Studio 2019 

Setup Application

  1. Open Visual Studio 2019 IDE
  2. Add New Project
  3. Select Blazor App

    Exploring Blazor Web Assembly App

  4. Give the project a name
  5. Select Blazor WebAssembly App, Un-tick ASP.NET Core hosted.

    Exploring Blazor Web Assembly App

  6. Click next and start the app to check if everything works correctly.

Exploring Project Structure

 
The default project structure looks like this,
 
Exploring Blazor Web Assembly App 
 
A few important files:
 
wwwroot → index.html
 
Index.html
 
This file contains all the dependencies that are essential to host the Blazor app on the client side and all the static assets required globally such as:
  1. <link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />  
  2. <link href="css/site.css" rel="stylesheet" />  
Hosting dependencies
  1. <script src="_framework/blazor.webassembly.js"></script>  
And a base url for all the relative URL’s used for Single Page Applications.
  1. <base href="/" />   
Startup.cs
 
It defines the bootstrapping component for application start and html tag component to target to run that component.
 
In this case <app></app> will be rendered to the app component.
 
The bootstrapping component will be App.razor which by convention translates to App.
 
App.razor
  1. <Router AppAssembly="@typeof(Program).Assembly">  
  2.     <Found Context="routeData">  
  3.         <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />  
  4.     </Found>  
  5.     <NotFound>  
  6.         <LayoutView Layout="@typeof(MainLayout)">  
  7.             <p>Sorry, there's nothing at this address.</p>  
  8.         </LayoutView>  
  9.     </NotFound>  
  10. </Router>  
The above file is App.razor,
 
Now App.razor uses Router Component which is part of Microsoft.AspNetCore.Components.Routing; it defines Route Handling mechanisms for the application. It contains two tags to handle found and 404 not found scenarios.
 
If route is found it will be handled in MainLayout. This class, once defined, does not need to be changed frequently. In a rare case we will be changing the contents of this file.
 
Let’s look at MainLayout as it is part of RouteView Component.
 
MainLayout.razor
 
It contains @inherits LayoutComponentBase, <NavMenu /> and @Body razor syntax based tag.
 
LayoutComponentBase is an abstract class defined in Microsoft.AspNetCore.Components. It contains Body Parameter that is used by this component.
 
LayoutComponentBase class inherits from ComponentBase class that contains all the events that are handled in the lifecycle of an application.
 
Here NavMenu is custom component defined. But @Body is mandatory to render content based on what we want to render inside this layout structure.
 
Now let us look at NavMenu.razor to check how navigation in client applications work.
 
NavMenu.razor
 
NavMenu component contains many <NavLink class=”” href=””>LinkName</NavLink> that defines the routes and their links corresponding to a particular route. They work due to the <Router></Router> Tag which is present in application. That tells us that this application uses routing.
 
Microsoft.AspNetCore.Components.Routing has 2 important components: <Router/> and <NavLink/> . We will use <NavLink/> more frequently. As Router, once defined, will not be altered in App.razor class, NavLink must only be used when navigating between the pages. Instead of <a> tags.
 
Outside the <NavMenu> also we will be using <NavLink> only to navigate between the links.
 

Working with Page Components like: Index.razor / FetchData.razor / Counter.razor

 
These are the application pages. They define all the UI logic and correspond to a particular route.
  • @page "/"
  • @page "/fetchdata"
  • @page "/counter"
This is how routes are mapped to corresponding page components.
 
We use  @page, which is Razor markup for defining a page and its URL.
 
Exploring Blazor Web Assembly App 
 
@code{ } is a code block.
 
More than one code block can be defined. They contain properties and fields that are state of the component. They contain event logics and other logic.
 

Events

 
The event handling is done in @code block.
 
The events available are:
  • OnInitializedAsync and OnInitialized
  • OnParametersSetAsync and OnParametersSet
  • OnAfterRenderAsync and OnAfterRender
  • ShouldRender
  • StateHasChanged
  • @implements IDisposable to Dispose()
You can find in-depth information about the events here.