Introduction to Blazor WebAssembly
Blazor WebAssembly is Microsoft’s new single-page application (SPA) framework for building interactive web applications on .NET Framework. Since it is built on .NET Framework, Blazor WebAssembly allows you to run C# code on the client as well as the server. Therefore, instead of being forced to write JavaScript on the client, we can now use C# everywhere.
Blazor is red hot!
Run C# on the client.
Goodbye, JavaScript!
In this chapter, we will explain the benefits of using the Blazor framework. We will introduce the three different Blazor hosting models and discuss both the advantages and disadvantages of each of them. Also, we will discuss the goals of WebAssembly and share where it is supported. Finally, we will guide you through the process of setting up your computer to complete the projects in this book.
In this chapter, we will cover the following topics:
- Benefits of using the Blazor framework
- Hosting models:
- Blazor Server
- Blazor Hybrid
- Blazor WebAssembly
- Differences between the Blazor hosting models
- What is WebAssembly?
- Setting up your PC
Benefits of using the Blazor framework
Using the Blazor framework offers several benefits. For starters, it is a free and open-source framework built on Microsoft’s robust and mature .NET Framework. Also, it is a SPA framework that uses Razor syntax and can be developed using Microsoft’s exceptional tooling. Finally, Microsoft is actively supporting and updating the Blazor framework. Let’s examine each of these benefits in detail in the following sections.
.NET Framework
The Blazor framework is built on .NET Framework. Therefore, anyone familiar with .NET Framework can quickly become productive using the Blazor framework. The Blazor framework leverages the robust ecosystem of .NET libraries and NuGet packages from .NET Framework. Also, since the code for both the client and server can be written in C#, the client and server can share code and libraries. For example, the client and server can share the application logic used for data validation.
Open source
The Blazor framework is open source. Since Blazor is a feature of the ASP.NET framework, all the source code for Blazor is available on GitHub as part of the dotnet/aspnetcore
repository, which is owned by the .NET Foundation. The .NET Foundation is an independent, non-profit organization established to support the innovative, commercially friendly, open-source ecosystem around the .NET platform. The .NET platform has a strong community of over 100,000 contributions from more than 3,700 companies.
Since .NET Framework is free, this means that Blazor is also free. There are no fees or licensing costs associated with using Blazor, including for commercial use.
SPA framework
The Blazor framework is a SPA framework. As the name implies, a SPA is a web app that consists of a single page. The application dynamically rewrites the areas of the page that have changed instead of loading an entirely new page in response to each UI update. The goal is faster transitions that make the web app feel more like a native app.
When a page is rendered, Blazor creates a render tree that is a graph of the components on the page. It is like the Document Object Model (DOM) created by the browser. However, it is a virtual DOM. Updates to the UI are applied to the virtual DOM and only the differences between the DOM and the virtual DOM are rendered by the browser.
Razor syntax
Razor is the ASP.NET view engine used to create dynamic web pages with C#. Razor is a syntax for combining HTML markup with C# code that was designed for developer productivity. It allows the developer to use both HTML markup and C# in the same file.
Blazor web apps are built using Razor components. Razor components are reusable UI elements that contain C# code, markup, and other Razor components. Razor components are quite literally the building blocks of the Blazor framework. For more information on Razor components, refer to Chapter 2, Building Your First Blazor WebAssembly Application.
IMPORTANT NOTE
Razor Pages and MVC also use the Razor syntax. Unlike Razor Pages and MVC, which render the whole page, Razor components only render the DOM changes. One way to easily distinguish between them is that Razor components use the RAZOR
file extension, while both MVC and Razor Pages use the CSHTML
file extension.
The name of the Blazor framework has an interesting origin story. The term Blazor is a combination of the word browser and the word razor.
Awesome tooling
You can use either Microsoft Visual Studio or Microsoft Visual Studio Code to develop Blazor applications. Microsoft Visual Studio is an integrated development environment (IDE), while Microsoft Visual Studio Code is a lightweight, yet powerful, editor. They are both incredible tools for building enterprise applications. As an added bonus, there are versions of both tools that are available for free.
Supported by Microsoft
Although the Blazor framework is open source, it is maintained by Microsoft. They continue to make large investments in the future of Blazor. The following list includes features that Microsoft is actively working on adding to Blazor:
- Hot reload improvements
- Ahead-of-time (AOT) compilation performance improvements
- Authentication improvements
- Additional built-in components
- Multithreading
There are many benefits associated with using the Blazor framework to develop web applications. Since it is built on .NET Framework, it enables developers to use the skills, such as C#, and the tools, such as Visual Studio, that they have already mastered. Also, since it is a SPA framework, Blazor web apps feel like native apps to the user. Finally, Microsoft is making a large investment in the future of Blazor.
Hosting models
As we mentioned earlier, Razor components are the building blocks of Blazor applications. Where those Razor components are hosted varies depending on the hosting model.
Blazor has three different hosting models:
- Blazor Server
- Blazor Hybrid
- Blazor WebAssembly
The first hosting model that Microsoft released was Blazor Server. In this hosting model, the Razor components are executed on the server. The second hosting model that Microsoft released, and the topic of this book, is Blazor WebAssembly. In this hosting model, the Razor components are executed on the browser using WebAssembly. The newest hosting model is Blazor Hybrid. Blazor Hybrid allows you to build native client apps by hosting the Razor components in an embedded Web View control.
Each hosting model has its own advantages and disadvantages. However, they all rely upon the same underlying architecture. Therefore, it is possible to write and test your code independently of the hosting model.
The major differences between the hosting models concern where the code executes, latency, security, payload size, and offline support. The one thing that all the hosting models have in common is the ability to execute at near native speed.
Blazor Server
As we just mentioned, the Blazor Server hosting model was the first hosting model released by Microsoft. It was released as part of the .NET Core 3 release in September 2019.
The following diagram illustrates the Blazor Server hosting model:

Figure 1.1: Blazor Server
In this hosting model, the web app is executed on the server and only updates to the UI are sent to the client’s browser. The browser is treated as a thin client and all the processing occurs on the server. Therefore, this model requires a constant connection to the server. When using Blazor Server, UI updates, event handling, and JavaScript calls are all handled over an ASP.NET Core SignalR connection.
IMPORTANT NOTE
SignalR is a software library that allows the web server to push real-time notifications to the browser. Blazor Server uses it to send UI updates to the browser.
Advantages of Blazor Server
There are a few advantages of using Blazor Server versus using Blazor WebAssembly. However, the key advantage is that everything happens on the server. Since the web app runs on the server, it has access to everything on the server. As a result, security and data access are simplified. Also, since everything happens on the server, the assemblies (DLLs) that contain the web app’s code remain on the server.
Another advantage of using Blazor Server is that it can run on thin clients and older browsers, such as Internet Explorer, that do not support WebAssembly.
Finally, the initial load time for the first use of a web app that is using Blazor Server can be much less than that of a web app that is using Blazor WebAssembly because there are much fewer files to download.
Disadvantages of Blazor Server
The Blazor Server hosting model has several disadvantages versus Blazor WebAssembly. The biggest disadvantage is that the browser must maintain a constant connection to the server. Since there is no offline support, every single user interaction requires a network roundtrip. As a result of all these roundtrips, Blazor Server web apps have higher latency than Blazor WebAssembly web apps and can feel sluggish. Also, network interruptions may cause a client to unexpectedly disconnect.
Another disadvantage of using Blazor Server is that it relies on SignalR for every single UI update. Microsoft’s support for SignalR has been improving, but it can be challenging to scale. When too many concurrent connections to the server are open, connection exhaustion can prevent other clients from establishing new connections.
Finally, a Blazor Server web app must be served from an ASP.NET Core server.
Blazor Hybrid
The Blazor Hybrid hosting model is the most recent hosting model released by Microsoft. It was released as part of the .NET 6 release in November 2021.
The following diagram illustrates the Blazor Hybrid hosting model:

Figure 1.2: Blazor Hybrid
In this model, the Razor components run in an embedded Web View control. Blazor Hybrid apps include Windows Forms, WPF, and .NET MAUI apps. By using the Blazor Hybrid hosting model, your apps have full access to the native capabilities of the devices that you choose to target.
Advantages of Blazor Hybrid
The advantage of using this model versus Blazor WebAssembly is that it does not require WebAssembly. Also, since the component’s C# code is executed in the host process, the Blazor Hybrid apps have access to the native capabilities of the device.
Disadvantages of Blazor Hybrid
The major disadvantage of using Blazor Hybrid is that they are hosted in a Web View component in the native app. So, the developer must know how to develop each type of native client app that they want to target. Another disadvantage is that they usually require a server to deliver the app. In contrast, a Blazor WebAssembly app can be downloaded as a set of static files.
Blazor WebAssembly
The Blazor WebAssembly hosting model is the topic of this book.
Blazor WebAssembly 3.2.0 was released in May 2020. Blazor WebAssembly in .NET 5 was released as part of the .NET 5.0 release in November 2020. ASP.NET Core Blazor was released as part of the .NET 6.0 release in November 2021, and it is a long-term support (LTS) release. The most recent release of Blazor WebAssembly was released as part of the .NET 7 release in November 2022 This book will be using Blazor WebAssembly in .NET 7 for all the projects.
TIP
LTS releases are supported by Microsoft for at least 3 years after their initial release. Blazor WebAssembly in .NET 7 is a current release rather than an LTS release. Current releases get free support and patches for 18 months. We recommend that if you are starting a new project with Blazor WebAssembly, you should use the most recent release.
The following diagram illustrates the Blazor WebAssembly hosting model:

Figure 1.3: Blazor WebAssembly
In this hosting model, the web app is executed on the browser. For both the web app and the .NET runtime to run on the browser, the browser must support WebAssembly. WebAssembly is a web standard supported by all modern browsers, including mobile browsers. While Blazor WebAssembly itself does not require a server, the web app may require one for data access and authentication.
In the past, the only way to run C# code on the browser was to use a plugin, such as Silverlight. Silverlight was a free browser plugin provided by Microsoft. It was very popular until Apple decided to forbid the use of any browser plugins on iOS. As a result of Apple’s decision, Silverlight was abandoned by Microsoft.
IMPORTANT NOTE
Blazor does not rely on plugins or recompiling the code into other languages. Instead, it is based on open web standards and is supported by all modern browsers, including mobile browsers.
Advantages of Blazor WebAssembly
Blazor WebAssembly has many advantages. First, since it runs on the browser, it relies on client resources instead of server resources. Therefore, the processing is offloaded to the client. Also, unlike Blazor Server, there is no latency due to each UI interaction requiring a roundtrip to the server.
Blazor WebAssembly can be used to create a Progressive Web App (PWA). A PWA is a web app that looks and feels like a native application. They provide offline functionality, background activity, native API layers, and push notifications. They can even be listed in the various app stores. By configuring your Blazor WebAssembly app as a PWA, your app can reach anyone, anywhere, on any device with a single code base. For more information on creating a PWA, refer to Chapter 6, Building a Weather App as a Progressive Web App (PWA).
Finally, a Blazor WebAssembly web app does not rely on an ASP.NET Core server. In fact, it is possible to deploy a Blazor WebAssembly web app via a Content Delivery Network (CDN).
Disadvantages of Blazor WebAssembly
To be fair, there are some disadvantages when using Blazor WebAssembly that should be considered. For starters, when using Blazor WebAssembly, the .NET runtime, the dotnet.wasm
file, and your assemblies need to be downloaded to the browser for your web app to work. Therefore, the first time you run a Blazor WebAssembly application it usually takes longer to initially load than an identical Blazor Server application. However, there are strategies that you can use to speed up the initial load time, such as deferring the loading of some of the assemblies until they are needed. Also, this is only an issue during the initial load since subsequent runs of the application will access the files from a local cache.
Another disadvantage of Blazor WebAssembly web apps is that they are only as powerful as the browser that they run on. Therefore, thin clients are not supported. Blazor WebAssembly can only run on a browser that supports WebAssembly. Luckily, due to a significant amount of coordination between the World Wide Web Consortium (W3C) and engineers from Apple, Google, Microsoft, and Mozilla, all modern browsers support WebAssembly.
Hosting model differences
The following table indicates the differences between the three models:
Blazor WebAssembly |
Blazor Hybrid |
Blazor Server |
|
Native execution speed |
X |
X |
X |
Executes on client |
X |
X |
|
Executes on server |
X |
||
Low latency after initial load time |
X |
X |
|
Fast initial load time |
X |
||
Offline support |
X |
X |
|
Does not require a server |
X |
||
Requires constant connection to a server |
X |
||
Can build PWAs |
X |
||
Assemblies sent to client |
X |
X |
|
Assembles remain on server |
X |
||
Can access native client features |
X |
||
Requires WebAssembly |
X |
||
Requires SignalR |
X |
||
Can run on thin clients |
X |
Table 1.1: Hosting model differences
The Blazor framework provides three different hosting models, Blazor Server, Blazor Hybrid, and Blazor WebAssembly. A Blazor Server web app runs on the server and uses SignalR to serve the HTML to the browser. A Blazor Hybrid web app runs in a Web View control in the native app. A Blazor WebAssembly web app runs directly in the browser using WebAssembly. They each have their advantages and disadvantages. However, if you want to create interactive, highly responsive, native-like web apps that can work offline, we recommend Blazor WebAssembly. Let’s learn more about WebAssembly in the next section.
What is WebAssembly?
WebAssembly is a binary instruction format that allows code written in high-level languages, such as C#, to run on the browser at near-native speed. To run .NET binaries in a web browser, it uses a version of the .NET runtime that has been compiled to WebAssembly. You can think of it as executing natively compiled code in a browser.
WebAssembly is an open standard developed by a W3C Community Group. It was originally announced in 2015, and the first browser that supported it was released in 2017.
WebAssembly goals
When WebAssembly was originally being developed, there were four main design goals for the project. This is a list of the original goals for WebAssembly:
- Fast and efficient
- Safe
- Open
- Don’t break the web
WebAssembly is fast and efficient. It is designed to allow developers to write code in any language that can then be compiled to run in the browser. Since the code is compiled, it is fast and performs at near-native speed.
WebAssembly is safe. It does not allow direct interaction with the browser’s DOM. Instead, it runs in its own memory-safe, sandboxed execution environment. You must use JavaScript interop to interact with the DOM. The project in Chapter 5, Building a Local Storage Service Using JavaScript Interoperability (JS interop), will teach you how to use JavaScript interop.
WebAssembly is open. Although it is a low-level assembly language, it can be edited and debugged by hand.
WebAssembly didn’t break the web. It is a web standard that is designed to work with other web technologies. Also, WebAssembly modules can access the same Web APIs that are accessible from JavaScript.
Overall, WebAssembly was able to meet all of the original goals and rapidly gained support from all of the modern browsers.
WebAssembly support
As mentioned earlier, WebAssembly runs on all modern browsers, including mobile browsers. As you can see from the following table, all current versions of the most popular browsers are compatible with WebAssembly:
Browser |
Version |
Microsoft Edge |
Current |
Mozilla Firefox, including Android |
Current |
Google Chrome, including Android |
Current |
Safari, including iOS |
Current |
Opera, including Android |
Current |
Microsoft Internet Explorer |
Not Supported |
Table 1.2: WebAssembly browser compatibility
IMPORTANT NOTE
Microsoft Internet Explorer is no longer supported by Microsoft as of June 15, 2022. It does not support WebAssembly and will never support WebAssembly.
WebAssembly is a web standard that allows developers to run code written in any language in the browser. It is supported by all modern browsers.
Now that we have discussed the benefits of using the Blazor framework and compared the various hosting models, it’s time to start developing using the Blazor WebAssembly framework. However, before we can get started, you need to set up your PC.
Setting up your PC
For the projects in this book, we use Microsoft Visual Studio Community 2022, .NET 7, Microsoft SQL Server 2022 Express Edition, and Microsoft Azure.
All the projects are built using Microsoft Visual Studio Community 2022 (64-bit) – Current Version 17.4.2 with the ASP.NET and Web Development workload. If you need to install Microsoft Visual Studio Community 2022, follow the directions in the Installing Microsoft Visual Studio Community Edition section later in this chapter.
TIP
Although we are using Microsoft Visual Studio Community 2022, any edition of Microsoft Visual Studio 2022 can be used to complete the projects in this book. Microsoft Visual Studio Code can also be used. However, all the screenshots are from Microsoft Visual Studio Community 2022.
Blazor WebAssembly in .NET 7 requires .NET 7.0. To determine the version of .NET that is running on your computer, open Command Prompt and enter the following command:
dotnet –-version
If your computer is not running .NET 7.0 or higher, follow the directions in the Installing .NET 7.0 section later in this chapter.
Chapters 3 and 10 use Microsoft Azure. Chapter 3 uses Microsoft Azure to publish a Blazor WebAssembly application and Chapter 10 uses Microsoft Azure Active Directory to secure a Blazor WebAssembly application.
The final two projects in this book use Microsoft SQL Server 2022 Express Edition as the backend database. If you need to install Microsoft SQL Server Express Edition, follow the directions in the Installing Microsoft SQL Server Express section later in this chapter.
TIP
Although we are using Microsoft SQL Server 2022 Express Edition, any year or edition of SQL Server can be used to complete the projects in this book.
Installing Microsoft Visual Studio Community Edition
Microsoft Visual Studio Community Edition is the free edition of Microsoft Visual Studio. To install Microsoft Visual Studio Community Edition, perform the following steps:
- Download the Visual Studio installer from https://visualstudio.microsoft.com.

Figure 1.4: Download Visual Studio selector
- Once the download is complete, run the installer to complete the installation.
- During the first step in the installation process, the Visual Studio installer will check the system for existing versions of Visual Studio. Once the installer has finished checking for installed versions, it will open the following installation dialog:

Figure 1.5: The Visual Studio installer
- Select the ASP.NET and web development workload and click the Install button to complete the installation.
Installing .NET 7.0
To install .NET 7.0, perform the following steps:
- Download the .NET 7.0 installer from https://dotnet.microsoft.com/download/dotnet/7.0. We are using the Windows x64 SDK Installer.
- Once the download completes, run the installer to complete the installation of .NET 7.0 on your computer.
- Open Command Prompt and enter the following command to verify that your computer is now running .NET 7.0:
dotnet –-version
The following screenshot is from a computer that is running .NET 7.0:
Figure 1.6: .NET version
Installing Microsoft SQL Server Express
Microsoft SQL Server Express is the free edition of Microsoft SQL Server. To install Microsoft SQL Server Express, do the following:
- Download the Microsoft SQL Server installer for SQL Server Express from https://www.microsoft.com/en-us/sql-server/sql-server-downloads.
- After the download completes, run the SQL Server installer.
- Select the Basic installation type:

Figure 1.7: The SQL Server installer
- Click the Accept button to accept the Microsoft SQL Server License Terms.
- Click the Install button to complete the installation.
The following screenshot shows the dialog that appears after SQL Server Express has been successfully installed:
Figure 1.8: SQL Server Express Edition
Create a Microsoft Azure account
Microsoft Azure is Microsoft’s cloud platform and it offers over 200 products and cloud services. You can use it to run and manage applications with the tools and frameworks of your choice.
If you do not already have a Microsoft Azure account, you can create a free account. Each free account comes with a generous $200 credit to get you started and over 55+ free services.

Figure 1.9: Microsoft Azure
To create a free Microsoft Azure account, do the following:
- Navigate to the Microsoft Azure page, https://azure.microsoft.com/.
- Click the Free account button.
- Click the Start free button.
- Complete the agreement and click the Sign up button.
To complete all the projects in this book, you will need a code editor, such as Microsoft Visual Studio Community 2022, .NET 7.0, Microsoft Azure, and Microsoft SQL Server. In this chapter, we showed you how to install Visual Studio 2022 Community Edition, .NET 7.0, and SQL Server 2022 Express Edition. We also showed you how to open a free Microsoft Azure account.
Summary
After completing this chapter, you should understand the benefits of using Blazor WebAssembly versus other Blazor hosting models and be prepared to complete the projects in this book.
In this chapter, we introduced the Blazor framework. The Blazor framework is built on .NET Framework and allows web developers to use C# on both the client and the server of a web app.
After that, we compared Blazor WebAssembly with both Blazor Server and Blazor Hybrid. All three hosting models are used to host Razor components. They each have their own advantages and disadvantages. We prefer Blazor WebAssembly.
In the last part of the chapter, we explained how to set up your computer with Microsoft Visual Studio Community Edition, .NET 7.0, and Microsoft SQL Server Express and how to open a Microsoft Azure account, all of which are required to complete the projects in this book.
Now that your computer is set up to complete the projects in this book, it is time to get started. In the next chapter, you will create your first Blazor WebAssembly web app.
Questions
The following questions are provided for your consideration:
- Which of the following hosting models requires a constant connection to a server: Blazor WebAssembly, Blazor Server, or Blazor Hybrid?
- Does using Blazor WebAssembly mean that you will never need to write JavaScript ever again?
- Does Blazor WebAssembly require any plugins to be installed on the browser?
- How much does it cost to get started developing with Blazor WebAssembly?
Further reading
The following resources provide more information concerning the topics in this chapter:
- For more information on Blazor, refer to https://blazor.net.
- For more information on the .NET Foundation, refer to https://dotnetfoundation.org.
- For more information on the ASP.NET repository on GitHub, refer to https://github.com/dotnet/aspnetcore.
- For general information on WebAssembly, refer to https://webassembly.org.
- For more information on browser compatibility with WebAssembly, refer to https://caniuse.com/?search=wasm.
- For more information on Razor Pages, refer to https://learn.microsoft.com/en-us/aspnet/core/razor-pages.
Join our community on Discord
Join our community’s Discord space for discussions with the author and other readers:
https://packt.link/BlazorWASM2e