avatarFuji Nguyen

Summary

This article provides a step-by-step guide on creating a Blazor WebAssembly application using Visual Studio 2019 and .NET 5.

Abstract

The article titled "Create your First Blazor WebAssembly App in .NET 5 using Visual Studio 2019" is a comprehensive tutorial aimed at developers interested in building Single Page Applications (SPAs) with C# instead of JavaScript. It introduces Blazor WebAssembly as a UI framework that leverages the power of C# and .NET on the client side within the browser. The tutorial outlines the necessary prerequisites, including basic web app knowledge, .NET experience, and local installations of .NET Core SDK and Visual Studio. It is divided into two main parts: configuring the local development environment with Visual Studio 2019 and the .NET 5 SDK, and creating a Blazor WebAssembly application through Visual Studio's project templates. The article provides detailed instructions and visual aids for setting up the development environment, updating Visual Studio, and configuring the new Blazor project with options for authentication and progressive web application features. The tutorial concludes with a note on the availability of the source code on GitHub and a teaser for a follow-up tutorial on securing the Blazor application with authentication and authorization.

Opinions

  • The author positions Blazor WebAssembly as a viable alternative to traditional JavaScript-based SPA frameworks by emphasizing the use of C#.
  • The tutorial is structured to be beginner-friendly, assuming only basic knowledge of web apps and C#.
  • Visual Studio 2019 is presented as a key productivity tool for Blazor development, with project templates and updates that streamline the development process.
  • The inclusion of visual aids (screenshots) is intended to enhance the learning experience and provide clear guidance.
  • The author suggests that Blazor can be used to create not just web applications but also apps that mimic desktop application behavior through the Progressive Web Application option.
  • By mentioning the next tutorial on authentication and authorization, the author implies that security is an important aspect of Blazor development that should not be overlooked.

Create your First Blazor WebAssembly App in Net 5 using Visual Studio 2019

Photo by Vika Aleksandrova on Unsplash

Browser + Razor = Blazor

Introduction

Blazor WebAssembly is a Microsoft UI framework for building Single Page Application (SPA) using C# rather than JavasScript. Microsoft provides productivity tools to scaffold the Blazor WebAssembly App via the dotnet command line and Visual Studio (VS) template. This tutorial will focus on using VS 2019 template to create the Blazor app.

Tutorial Content

In this tutorial, you will learn to set up your development environment and build your first Blazor web app with Visual Studio 2019.

Part 1: Configure your local environment for Blazor development with Visual Studio 2019.

Part 2: Create a Blazor WebAssembly App using Visual Studio 2019.

Prerequisites

  • Basic knowledge of web app concepts
  • C# .NET experience at a beginner level
  • Local installations of the .NET Core SDK and Visual Studio

Part 1 — Configure your local environment for Blazor development with Visual Studio 2019

Part 1 is divided into two tasks:

  1. Install .NET 5 SDK
  2. Update the Visual Studio (if needed) to support Blazor WebAssembly development

Task 1.1 — Install the latest .NET 5.0 SDK version (version 5.0.100 or greater).

.NET is made up of the runtime and the SDK. The runtime is used to run a .NET app and may or may not be included with the app. The SDK is used in development to create .NET apps and libraries. To install the SDK:

  1. Visit the .NET 5.0 SDK site.
  2. Select an installer with the OS matching your workstation. See Figure 1 for visual aids.
Figure 1 — Selection of .NET 5 SDK installer

Task 1.2 — Update Visual Studio 2019 to Version 16.8.2 or Greater

At the time of this write-up, the Visual Studio is at version 16.10.0. To update Visual Studio to the most recent release,

  1. While in Visual Studio, click on Help menu
  2. Choose Check for Updates. See Figure 2 for visual aids.
  3. In the Update available dialog box, choose Update. See Figure 3 for visual aids.
Figure 2— Navigation to “Check for Updates) from the Visual Studio Help menu
Figure 3— Visual Studio Updates screen in

Part 2 — Create Blazor App using Visual Studio 2019

The easiest way to create a new project is to start from a project template for a particular type of application or website. A project template consists of a basic set of pre-generated code files, config files, assets, and settings.

When you first open Visual Studio, the start window appears, and from there, you can choose to create a new project.

To create new Blazor Web Assembly App (see Figure 4 for visual aids)

  1. Open Visual Studio 2019
  2. Select Create a New Project
  3. Search for Blazor and select Blazor WebAssembly App
  4. Click Next
Figure 4 — Create a new project screen

On Create your new project screen (see Figure 5 for visual aids)

  1. Enter project name MyFirstBlazorWebAssembly
  2. Enter location C:\apps\devkit\Clients
  3. Accept all the default values in the Solution and Solution name fields
  4. Click on Next
Figure 5 — Configure your new project screen

On the Additional information screen (see Figure 6 for visual aids)

  1. Select .NET 5.0 in the Target Framework
  2. Select Individual Accounts for the Authentication Type. This will enable authentication code (ex. Log in link to the menu bar)
  3. Optionally — If you want to create the app to behave like a desktop app, select the Progressive Web Application checkbox
  4. Optionally — If you want to create the app for deployment with a backend app to serve its files, select the ASP.NET Core hosted checkbox
  5. Click on Create
Figure 6 — Additional information

The new Blazor WebAssembly app is created and open in Visual Studio. See Figure 7 for visual aids.

Figure 7— Blazor WebAssembly App solution in Visual Studio 2019

To run the Blazor app, Hit F5. It opens the Blazor App in the web browser. See Figure 8 for visual aids.

Figure 8 — Blazor WebAssembly running in the browser

Congratulation!!! You have successfully created your first Blazor WebAssembly App.

Summary

In this tutorial, you learned how to create Blazor WebAssembly App. The source code in this tutorial is available on GitHub.

What can you learn next? When selecting the option Individual Accounts for Authentication Type (see Figure 6), the generated code includes the Log in option in the upper right corner of the app header (see Figure 8). However, when clicking on the link, it does not work. My next tutorial will address authentication and authorization.

Blazor Webassembly
Net Core 5
C Sharp Programming
Single Page Applications
Visual Studio
Recommended from ReadMedium