On the surface, a website created with Blazor is fast, reliable, and capable of performing complex tasks directly in the browser. Breaking it down, a Blazor website consists of multiple components, each serving a specific role - some display the login area, some handle user interactions, and so on. This tutorial will provide you with an overview of Razor components:
A component in Blazor is a reusable building block that defines a part of a webpage's UI and behavior. It is written using Razor syntax, which combines C# and HTML. Each component can have its own logic, styling, and interactions, making it an essential unit for developing Blazor applications. Components can be as simple as a button or as complex as an entire form. They can also communicate with each other and manage data efficiently, allowing for a modular and maintainable architecture.
You can see in the image below that we have broken down the UI of Blazor School's homepage into individual components.
A Razor Component (.razor) is different from a Razor Page (.cshtml). While both use Razor syntax, they serve distinct purposes in different contexts. A Razor Page is used in ASP.NET MVC, whereas a Razor Component is designed for building reusable UI elements in Blazor. Additionally, Blazor does not use Razor Pages as components.
A component typically consists of three sections:
There are 3 types of components:
@page
directive and allows URL parameters to be passed. It is designed to function as a standalone page rather than being used within another component. Those components are not designed to be reused.@page
directive and is meant to be used inside another component to solve specific problems. Those components are design to be reused.LayoutComponentBase
and defines the layout structure of a page or website.