C# tutorials > Frameworks and Libraries > ASP.NET Core > Blazor for client-side web UI with C# (components, routing, data binding)
Blazor for client-side web UI with C# (components, routing, data binding)
Blazor allows you to build interactive web UIs using C# instead of JavaScript. It runs your C# code on the browser using WebAssembly. This tutorial explores Blazor components, routing, and data binding.
Creating a Simple Blazor Component
This code snippet defines a simple Blazor component named `Counter`. The `@page "/counter"` directive specifies that this component is accessible at the `/counter` route. It displays a heading, the current count, and a button. When the button is clicked, the `IncrementCount` method is called, which increments the `currentCount` variable. Blazor automatically updates the UI when the value of `currentCount` changes.
@page "/counter"
<h1>Counter</h1>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}
Concepts Behind the Snippet
Components: Blazor applications are built from reusable UI components. Components are C# classes that represent a piece of the user interface. Routing: Blazor uses routing to navigate between different components. The `@page` directive specifies the route for a component. Data Binding: Blazor uses data binding to keep the UI in sync with the component's data. The `@onclick` attribute binds the button's click event to the `IncrementCount` method. The `@currentCount` syntax displays the value of the `currentCount` variable.
Data Binding Examples
This example demonstrates two-way data binding. The `@bind` attribute on the input element binds the input's value to the `message` variable. Whenever the user types something into the input, the `message` variable is updated, and the paragraph below is updated to reflect the new value.
@page "/databinding"
<h1>Data Binding Example</h1>
<input type="text" @bind="message" />
<p>You entered: @message</p>
@code {
private string message = "";
}
Real-Life Use Case Section
Imagine building a form with several input fields. Using Blazor's data binding, you can easily bind each input field to a property in your component. As the user fills out the form, the data is automatically updated in your C# code, making it easier to process the form data when the user submits it. Another common use is in creating interactive dashboards where data updates in real time based on user interactions. Blazor components can be used to build complex UI elements like grids, charts, and data tables, and data binding can be used to keep these elements synchronized with the underlying data sources.
Best Practices
Component Composition: Break down complex UIs into smaller, reusable components. This makes your code more modular and easier to maintain. Use Parameter Attributes: Use `[Parameter]` attributes to pass data between components. This makes your components more flexible and reusable. Avoid Excessive State Changes: Minimize the number of state changes in your components. Frequent state changes can lead to performance problems.
Interview Tip
When discussing Blazor in interviews, highlight your understanding of the component model, routing, and data binding. Be prepared to discuss the advantages and disadvantages of Blazor compared to other web UI frameworks like React, Angular, or Vue.js. Emphasize the benefits of using C# for both the server-side and client-side, such as code reuse and type safety.
When to Use Blazor
Blazor is a good choice when you want to:
Memory Footprint
Blazor's memory footprint depends on the hosting model (WebAssembly or Server) and the complexity of the application. Blazor WebAssembly apps download the .NET runtime and application assemblies to the browser, which can result in a larger initial download size. However, once loaded, the application runs entirely on the client-side, reducing server load.
Alternatives
Alternatives to Blazor include:
Pros
Cons
FAQ
-
What is Blazor?
Blazor is a web UI framework that allows you to build interactive client-side web applications using C# instead of JavaScript.
-
What are the two hosting models for Blazor?
Blazor supports two hosting models: Blazor WebAssembly (runs in the browser using WebAssembly) and Blazor Server (runs on the server and communicates with the browser using SignalR).
-
What is data binding in Blazor?
Data binding in Blazor is a way to keep the UI synchronized with the component's data. Changes to the data automatically update the UI, and changes in the UI automatically update the data.