Technical FAQ for Developers
Find answers for the most frequently asked questions
Razor and Blazor are both modern web development technologies from Microsoft. They can work together but serve different purposes. Razor is a popular template markup syntax for .NET. It is used to generate dynamic HTML on the server-side ASP.NET applications. In contrast, Blazor (Browser + Razor) is a .NET-based web framework that enables developers to build interactive single-page applications (SPA) using C# instead of JavaScript. Blazor applications can run either in the browser via WebAssembly or on the server using SignalR.
Blazor is Microsoft’s newest .NET web development framework. It allows developers to create single-page web applications that run in the browser using C# instead of JavaScript.
There are two types of Blazor apps:
Key features of Blazor:
When Developer Use Blazor:
These are the most common use cases in which developers prefer Blazor:
Blazor Example:
Below is an example of a data table component in Blazor
@page "/datatable"
<h3>Simple Data Table</h3>
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
<tbody>
@foreach (var item in Items)
{
<tr>
<td>@item.Id</td>
<td>@item.Name</td>
<td>@item.Price</td>
</tr>
}
</tbody>
</table>
@code {
private List<Product> Items = new List<Product>
{
new Product { Id = 1, Name = "Apple", Price = 0.99M },
new Product { Id = 2, Name = "Banana", Price = 0.59M },
new Product { Id = 3, Name = "Cherry", Price = 2.99M }
};
private class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
}
@page “/datatable” creates the component and it can be called and reused throughout a project.
Razor is a server-side templating engine part of the ASP.NET framework. Developers use if to leverage C# to render dynamic HTML content. Razor is the syntax used in both ASP.NET MVC and Blazor. Here is a simple example:
<h1> Hello @Model.FirstName </h1>
Razor takes care of rendering your HTML based on the data in your model, while also supporting various conditionals and loops. The “@” symbol indicates code to be executed on the server. In this case, a first name would be retrieved.
Key Features in Razor:
When Developers Use Razor:
Razor Example:
Here is an example of a data table created as a Razor page:
@model List<Product>
<table class="table table-bordered">
<thead>
<tr>
<th>Product ID</th>
<th>Name</th>
<th>Price</th>
<th>In Stock</th>
</tr>
</thead>
<tbody>
@foreach (var product in Model)
{
<tr>
<td>@product.Id</td>
<td>@product.Name</td>
<td>@product.Price.ToString("C")</td>
<td>
@if (product.InStock)
{
<span class="badge bg-success">Yes</span>
}
else
{
<span class="badge bg-danger">No</span>
}
</td>
</tr>
}
</tbody>
</table>
@code {
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public bool InStock { get; set; }
With very little C# inserted into the HTML, you easily create a dynamic table. The data set is defined with “@model” and each field is populated with very little code.
As previously discussed, Razor only renders on the server and Blazor can render on the client via WebAssembly or on the server.
The primary difference is interaction. When using Razor, an interaction event is sent to the server via AJAX, new HTML is rendered and sent back to the client requiring a page reload. When using Blazor interactions are handled real-time via SignalIR on the server or WebAssembly in the client.
For example, you may want to show the current time when a button is clicked.
In Razor, that interaction is sent to the service via the submit button.
@page
@model IndexModel
@{
ViewData["Title"] = "Welcome";
var CurrentTime = DateTime.Now.ToString("hh:mm:ss tt");
}
<h1>@ViewData["Title"]</h1>
<p>Current server time is: @CurrentTime</p>
<form method="post">
<button type="submit">Refresh</button>
</form>}
In Blazor, it is handled with the @onCLick directive.
@page "/time" <h1>Current Time</h1> <p>The time is: @CurrentTime</p> <button class="btn btn-primary" @onclick="UpdateTime">Refresh</button> @code { private string CurrentTime = DateTime.Now.ToString("hh:mm:ss tt"); private void UpdateTime() { CurrentTime = DateTime.Now.ToString("hh:mm:ss tt"); } }
A primary advantage of Blazor is that is built-on a component-based architecture similar to JavaScript frameworks like React. It’s what allows companies like Progress to produce Blazor Component libraries.
On the other hand, Razor is a templating engine, and its syntax is coded inline and doesn’t lend itself to reusability.
When we recommend choosing Blazor:
When we recommend choosing Razor: