free and open-source
Free and open-source software (FOSS) is software available under a Software license, license that grants users the right to use, modify, and distribute the software modified or not to everyone free of charge. FOSS is an inclusive umbrella term ...
web framework
A web framework (WF) or web application framework (WAF) is a software framework that is designed to support the development of web applications including web services, web resources, and web APIs. Web frameworks provide a standard way to build a ...
that enables developers to create web user interfaces (UI) based on components, using C# and
HTML
Hypertext Markup Language (HTML) is the standard markup language for documents designed to be displayed in a web browser. It defines the content and structure of web content. It is often assisted by technologies such as Cascading Style Sheets ( ...
. It is being developed by
Microsoft
Microsoft Corporation is an American multinational corporation and technology company, technology conglomerate headquartered in Redmond, Washington. Founded in 1975, the company became influential in the History of personal computers#The ear ...
, as part of the
ASP.NET Core
ASP.NET Core is an open-source modular web-application framework. It is a redesign of ASP.NET that unites the previously separate ASP.NET MVC and ASP.NET Web API into a single programming model. Despite being a new framework, built on a new web ...
web app framework.
Blazor can be used to develop single-page, mobile, or server-rendered applications using .NET technologies.
History
In 2017, at NDC Oslo, Steve Sanderson, Software engineer at Microsoft, unveiled an experimental client-side web application framework for
.NET
The .NET platform (pronounced as "''dot net"'') is a free and open-source, managed code, managed computer software framework for Microsoft Windows, Windows, Linux, and macOS operating systems. The project is mainly developed by Microsoft emplo ...
that he called "Blazor". The demo involved an interactive app running in the browser using
WebAssembly
WebAssembly (Wasm) defines a portable binary-code format and a corresponding text format for executable programs as well as software interfaces for facilitating communication between such programs and their host environment.
The main goal of ...
and a rudimentary development experience in Visual Studio. Sanderson demonstrated how to build interactive components using C# and
Razor
A razor is a bladed tool primarily used in the removal of body hair through the act of shaving. Kinds of razors include straight razors, safety razors, disposable razors, and electric razors.
While the razor has been in existence since be ...
syntax. The app was then compiled to .NET assemblies that were running on a lightweight third-party open-source .NET runtime, called DotNetAnywhere, that had been compiled to WebAssembly.
The name, "Blazor", as explained by Steve Sanderson, is a
portmanteau
In linguistics, a blend—also known as a blend word, lexical blend, or portmanteau—is a word formed by combining the meanings, and parts of the sounds, of two or more words together.
of the words "Browser" and "Razor". (from the
Razor
A razor is a bladed tool primarily used in the removal of body hair through the act of shaving. Kinds of razors include straight razors, safety razors, disposable razors, and electric razors.
While the razor has been in existence since be ...
syntax being used)
Blazor got admitted as an official open-source project by Microsoft, and in 2018, as part of .NET Core 3.1, Blazor Server was released to the public. It enabled server-driven interactive web app that updates the client browser via
WebSocket
WebSocket is a computer communications protocol, providing a full-duplex, simultaneous two-way communication channel over a single Transmission Control Protocol (TCP) connection. The WebSocket protocol was standardized by the Internet Engineering ...
s. Shortly thereafter, Blazor WebAssembly was released. Unlike the prototype, it used the Mono .NET runtime on WebAssembly. This is the same runtime that is used for developing mobile apps with .NET MAUI (previously
Xamarin
Xamarin is a Microsoft-owned San Francisco-based software company founded in May 2011 by the engineers that created Mono (software), Mono, Mono (software)#Xamarin.Android, Xamarin.Android (formerly Mono for Android) and Mono (software)#Xamarin.i ...
).
The Blazor source code was first located in its own repository on GitHub, until it was merged into the ASP.NET Core
monorepo
In version-control systems, a monorepo (" mono" meaning 'single' and "repo" being short for ' repository') is a software-development strategy in which the code for a number of projects is stored in the same repository. This practice dates back to ...
. The development has been carried out from there ever since.
With the release of .NET 5, Blazor stopped working on
Internet Explorer
Internet Explorer (formerly Microsoft Internet Explorer and Windows Internet Explorer, commonly abbreviated as IE or MSIE) is a deprecation, retired series of graphical user interface, graphical web browsers developed by Microsoft that were u ...
Microsoft Edge
Microsoft Edge is a Proprietary Software, proprietary cross-platform software, cross-platform web browser created by Microsoft and based on the Chromium (web browser), Chromium open-source project, superseding Edge Legacy. In Windows 11, Edge ...
.
In 2023, with .NET 8, Blazor on the server underwent some fundamental changes to enable server-side rendered (SSR) pages that are not fundamentally interactive, allowing Blazor to be used as an alternative to MVC Razor Pages. With this change, developers can opt-in per component (or page) whether it should be interactive, and whether it should run on the server or in the browser using WebAssembly. These are referred to as Interactive "Render modes".
Components
Components are formally referred to as ''Razor components''.
A Razor component consists mainly of HTML that is mixed with Razor templating syntax that enables the inline-use of C# to influence the rendering.
The Blazor component model makes sure that the rendered markup gets updated when the state of the component changes, usually in response to user action.
While both markup and C# code can be placed in the same .razor file, it is also possible to have a separate code-behind file with a partial class.
Components are compiled into .NET classes. The HTML and Razor markup of a component gets translated into code that builds a render tree that then drives the actual rendering.
Example
The following example shows how to implement a simple counter that can be incremented by clicking a button:
Counter
Count: @count
@code
Hosting models
Blazor apps can be hosted in multiple ways. These are the hosting models as of .NET 8.
Blazor Web app (Server)
A server-hosted Blazor app, as part of an ASP.NET Core app.
Static server-side rendering (SSR)
By default, components are rendered by the server as static HTML, without any interactivity. Interactivity can be enabled per component by setting a render mode.
This is equivalent to how MVC views and Razor Pages are rendered.
Render modes
Source:
In .NET 8, Blazor introduced the concept of render modes which configure whether Razor components are interactive and what drives that interactivity.
The render mode is inherited within a component hierarchy, from its top most parent component that has a set render mode. This can not be overridden by child components, unless its render mode is the default Static Server.
* Static Server – The component is rendered statically on the server with no interactivity. This is the default.
* Interactive Server – The component is running on the server in interactive mode. The interactivity is server-driven and changes are pushed to the client over
WebSocket
WebSocket is a computer communications protocol, providing a full-duplex, simultaneous two-way communication channel over a single Transmission Control Protocol (TCP) connection. The WebSocket protocol was standardized by the Internet Engineering ...
, using
SignalR
SignalR is a free and open-source software library for Microsoft ASP.NET that allows server code to send asynchronous notifications to client-side web applications. The library includes server-side and client-side JavaScript components.
Details
...
.
* Interactive WebAssembly – The component is running in interactive mode in the browser using WebAssembly.
* Interactive Auto – This will initially load the component in the Interactive Server render mode while the Blazor bundle is downloaded. On subsequent visits Interactive WebAssembly is used on the client.
Prerendering
Interactive components is pre-rendered on the server before being materialized on the client and interactivity kicking in. This behavior is turned on by default, but can be turned off.
Enhanced navigation
This feature makes navigation on a static site much smoother in a way that feels like a Single Page application (SPA).
When navigating from one static page to another, the app intercepts the navigation, retrieving just the content of the target page, and then apply only the changes to the DOM. That way the page doesn't flicker as it usually does when being completely reloaded upon navigating to another page.
WebAssembly (Standalone)
This is a standalone interactive WebAssembly app running in the client browser.
Upon navigating to the app in a browser, the app bundle get downloaded, then loaded and executed within the browser's sandbox.
A WebAssembly app can also be made into a
Progressive web app
A progressive web application (PWA), or progressive web app, is a type of web app that can be installed on a device as a standalone application software, application. PWAs are installed using the offline cache of the device's web browser.
PWAs w ...
(PWA).
Prior to .NET 8, there was a project template in which a Blazor WebAssembly app was hosted within an ASP.NET Core application containing Web APIs. This was removed in favor of the Blazor Web app project template, although the functionality still remains.
Hybrid
Allows Blazor apps to run within a native app using a WebView. Rendering is performed in the hosting process, without a web server.
Hybrid apps can be hosted in
Windows Presentation Foundation
Windows Presentation Foundation (WPF) is a free and open-source user interface framework for Windows-based desktop applications. WPF applications are based in .NET, and are primarily developed using C# and XAML.
Originally developed by Microso ...
or WinForms application.
This approach is used for building native mobile apps with Blazor, using .NET MAUI.
Server (Legacy)
The intended use was to enable server-driven interactive components, with changes being sent out to the client using WebSockets.
A component was rendered within a MVC Razor Page.
It also enabled prerendering of WebAssembly components.
This hosting model was formally referred to as "Blazor Server".
It has been deprecated in favor of Blazor Web app.
See also
* asm.js – precursor of WebAssembly enabling client-side web apps written in C or C++
*
Google Native Client
Google Native Client (NaCl) is a discontinued sandboxing technology for running either a subset of Intel x86, ARM, or MIPS native code, or a portable executable, in a sandbox. It allows safely running native code from a web browser, independ ...
– deprecated Google's precursor to WebAssembly that enables running native code in a web browser, independent of browser's
operating system
An operating system (OS) is system software that manages computer hardware and software resources, and provides common daemon (computing), services for computer programs.
Time-sharing operating systems scheduler (computing), schedule tasks for ...