This topic can help you understand Blazor Server: what Blazor Server is, what advances it provides, and what you might expect as you start to build your websites with Blazor Server.
Blazor Server is a single-page application framework, based on ASP.NET. As a framework, Blazor includes:
If you stick with Blazor Server, you are taking advantage of a framework that can scale from single-developer projects to enterprise-level. Blazor Server is designed to make your code well-organized, make things as simple as possible, so you can take advantage of the latest developments with a minimum of effort. Most of all, joining the .NET ecosystem means you are joining the biggest ecosystem in the world so that you can easily integrate with the cloud, making your business expand.
This section explains the core ideas of Blazor Sever. Understanding these ideas is very important because it can help you develop Blazor Server websites more effectively.
Components are the building blocks that compose a website. The component in Blazor Server is called Razor Component. A Razor Component includes HTML template and an @code{ }
block for C# codes to update the HTML template and styles. Here is an example of Razor Component.
HelloWorld.razor
contains HTML template and @code{ }
block.HelloWorld.razor.css
contains the CSS for the HTML template.The following is a minimal Razor Component.
<h2>Hello World!</h2> <p>This is my first component!</p> @code { // The code in this section drives the component's behaviour. }
<HelloWorld></HelloWorld>
<component type="typeof(HelloWorld)" render-mode="ServerPrerendered"></component>
<!--!--> <h2>Hello World!</h2> <p>This is my first component!</p> <!--!-->
Every Razor Component has an HTML template that declares how that component renders. Blazor Server extends HTML with additional syntax that lets you insert dynamic values from your component. Blazor Server automatically updates the rendered DOM when your component's state changes. Let's see this example:
<p>@Message</p>
@code{ }
block@code { private string Message = "Hello World!"; }
<p>Hello, World!</p>
Dependency Injection allows you to declare the dependencies of C# classes without caring about their initialization. Instead, Blazor Server handles the initialization for you. This design pattern allows you to write more testable and flexible code. Although understanding dependency injection is not required to use Blazor Server, we strongly recommend it as a best practice and many aspects of Blazor Server take advantage of it.
To illustrate how dependency injection works, start with the following simple example:
@using Microsoft.Extensions.Logging @inject ILogger<HelloWorld> Logger <h2>Hello World!</h2> @code { protected override void OnInitialized() { Logger.LogInformation("Dependency Injection is wonderful!"); } }
You will need to use the @inject
to inject classes from Blazor Server. Once it is injected, you can use it like another instance. In this example, the Logger
instance is injected from Blazor Server.
dotnet build | Builds a project and all of its dependencies. |
dotnet run | Runs source code without any explicit compile or launch commands. |
dotnet test | .NET test driver used to execute unit tests. |
dotnet publish | Publishes the application and its dependencies to a folder for deployment to a hosting system. |
For more information about the .NET CLI, see the Microsoft Docs.
This topic is intended to give you a brief overview of what Blazor Server is, what advances it provides, and what you might expect as you start to build your websites with Blazor Server.
To see Blazor Server in action, see our Getting Started tutorial. This tutorial contains an example solution so you can explore and learn.