Blazor Explained

Blazor
Logo Size:64px
Author:Microsoft
Developer:.NET Foundation
Operating System:Linux, macOS, Windows
Included With:ASP.NET Core
Genre:Web framework
License:Apache License 2.0

Blazor is a free and open-source web framework that enables developers to create web user interfaces (UI) based on components, using C# and HTML.[1] [2] [3] [4] [5] It is being developed by Microsoft, as part of the ASP.NET Core 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 [6] an experimental client-side web application framework for .NET that he called "Blazor". The demo involved an interactive app running in the browser using WebAssembly, and a rudimentary development experience in Visual Studio. Sanderson demonstrated how to build interactive components using C# and Razor 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 of the words "Browser" and "Razor". (from the Razor 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 update the client browser via WebSockets. 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).

The Blazor source code was first located in its own repository on GitHub, until it was merged into the ASP.NET Core monorepo. The development has been carried out from there ever since.

With the release of .NET 5, Blazor has stopped working on Internet Explorer and the legacy version of Microsoft Edge.[7]

In 2023, with .NET 8, Blazor on the server underwent some fundamental changes[8] 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:[9]

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.

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 (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.[10] Rendering is performed in the hosting process, without a web server.

Hybrid apps can be hosted in Windows Presentation Foundation 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

Further reading

External links

Notes and References

  1. Web site: Strahl. Rick. 31 July 2018. Web Assembly and Blazor: Re-assembling the Web. live. https://web.archive.org/web/20181022114639/https://weblog.west-wind.com/posts/2018/Jul/31/Web-Assembly-and-Blazor-Reassembling-the-Web. 22 October 2018. Rick Strahl's Weblog. Self-published.
  2. Web site: Tomassetti. Federico. September 4, 2018. Blazor: .NET in the Browser. live. https://web.archive.org/web/20181022115510/https://tomassetti.me/blazor-net-in-the-browser/. 22 October 2018. tomassetti.me. Strumenta.
  3. Web site: James. Mike. 12 February 2018. Blazor .NET In The Browser. live. i-programmer.info. Self-published. https://web.archive.org/web/20180218121850/http://i-programmer.info:80/news/89-net/11539-blazor-net-in-the-browser.html . 2018-02-18 .
  4. Miller. Jonathan. September 2018. C# in the Browser with Blazor. live. MSDN Magazine. 33. 9. https://web.archive.org/web/20181022123032/https://msdn.microsoft.com/en-us/magazine/mt829752.aspx. 22 October 2018.
  5. Web site: Roth. Daniel. 22 March 2018. Get started building .NET web apps that run in the browser with Blazor. live. ASP.NET Blog. Microsoft. https://web.archive.org/web/20190430042432/https://devblogs.microsoft.com/aspnet/get-started-building-net-web-apps-in-the-browser-with-blazor/ . 2019-04-30 .
  6. Web site: 2017-07-10 . Web Apps can’t really do *that*, can they? - Steve Sanderson . 2024-02-28 . YouTube.
  7. Web site: Roth . Daniel . 30 September 2020 . Updated Blazor browser support for .NET 5 . dotnet/aspnetcore repo . . GitHub.com.
  8. Web site: 2024-12-02 . ASP.NET Core Blazor fundamentals . 2024-02-28.
  9. Web site: 2024-02-09 . ASP.NET Core Blazor render modes . 2024-03-26.
  10. Web site: ASP.NET Core Blazor Hybrid . 2023-11-14.