Introduction

Blazor is a web framework developed by Microsoft that allows developers to build interactive web applications using C# instead of JavaScript. It is part of the .NET ecosystem, making it possible to run C# code directly in the browser with the help of WebAssembly, or on the server with a real-time connection. This cheatsheet is a quick guide to the most critical Blazor features, syntax, and usage examples.

Blazor Hosting Models

Blazor Server

Blazor WebAssembly

Components

Data Binding

1. One-way binding

  
    <p>Hello, @name</p>
@code {
    string name = "Rinki";
}
  

Data flows from code to UI.

2. Two-way binding

  
    <input @bind="name" />
<p>@name</p>

@code {
    string name = "Rinki";
}
  

Data syncs between UI and code.

Event Handling

Lifecycle Methods

Dependency Injection

Routing

Forms and Validation

State Management

  1. Local state (inside component): Use fields/properties.

  2. App-wide state: Use services registered as Scoped.

JavaScript Interop

Reusable Layouts

Error Handling

Conclusion

Blazor empowers developers to create modern web applications using C# instead of JavaScript. With features such as components, data binding, routing, forms, dependency injection, and JavaScript interop, Blazor brings the entire .NET ecosystem to web development. This cheatsheet covers the most valuable concepts for getting started and building scalable apps faster.