A Blazor application is a build-out of the things that make up websites, like HTML and CSS and also C# code. Blazor applications are built using Razor Components, which is an ASP.NET capability. These are the files consist of C# and HTML.
Fig.1 Sample Component Code
Let’s take an example, refer to Fig.1, the Counter.razor component has a button that triggers onclick method IncrementCount(). Both the codes reside on the same page and this is what code in Blazor applications look like, but we can also split the code and UI. The components are reusable blocks that you can use in your application and also you can nest your components.
Why should we use Blazor?
Fig.2 Blazor WebAssembly
When a Blazor application is built, it produces assemblies amongst the other things. These DLLs are .NET Standard DLLs that you can use in any other compatible application, which means you can create a Blazor Component Library and reuse it. These DLLs are everything that makes up your application and its dependencies like the system. DLLs.
Fig.3 Project Structure of Blazor WebAssembly
Fig.3 shows the basic project structure of a Blazor WebAssembly.
Getting Started with Blazor
There are different ways to create a Blazor application. We can create Blazor application using Visual Studiowhich gives access to a full suite of development tools that Microsoft has to offer. It does not matter which edition of the visual studio are we using.
We can also develop Blazor application using Visual Studio Code, the lighter version of Visual Studio. And finally, we can develop the Blazor application using a command-line interface.
First thing first, we need to download and install the latest version of the SDK and then pick the tool that we like to use. If we choose Visual Studio, then we need to make sure ASP.NET and Web Development Workload are Enabled.
If we choose Visual Studio Code, then Install the latest VS Code C# extension, and if we choose common-line, we can simply create by running dotnet new.
Demo using Visual Studio
Do the steps as follows,
Open Visual Studio
Choose to Create a new project option
Choose Blazor App as the project template
Configure the project by providing the location and meaningful name
Choose either Blazor Server Application or Blazor WebAssembly App
Now you have successfully created the Blazor Project
Blazor WebAssemby and Blazor Server
In the client-side model (Blazor WebAssembly), the Blazor app, its dependencies, and the .NET runtime are downloaded to the browser, and the app is executed directly on the browser UI thread. All UI updates and event handling happen within the same process.
In this article, we have discussed what Blazor is and how it works. I hope this article will help you to kickstart developing your own Blazor applications. Let's explore more about Blazor in upcoming articles.
Source: Medium, Wikipedia
The Tech Platform