| Blazor | |
|---|---|
| Original author | Microsoft |
| Developer | .NET Foundation |
| Initial release | 2018; 8 years ago (2018) |
| Operating system | Linux,macOS,Windows |
| Included with | ASP.NET Core |
| Type | Web framework |
| License | Apache License 2.0 |
| Website | dotnet |
| Repository | github |
Blazor is afree and open-sourceweb framework that enables developers to create web user interfaces (UI) based on components, usingC# andHTML.[1][2][3][4][5] It is being developed byMicrosoft, as part of theASP.NET Core web app framework.
Blazor can be used to developsingle-page, mobile, orserver-rendered applications using .NET technologies.
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 usingWebAssembly and a rudimentary development experience in Visual Studio. Sanderson demonstrated how to build interactive components usingC# andRazor 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 aportmanteau of the words "browser" and "razor" (from theRazor syntax being used).
Blazor was admitted as an official open-source project by Microsoft, and in 2018, as part of .NET Core 3.0, Blazor Server was released to the public.[7] It enabled server-driven interactive web app that updates the client browser viaWebSockets. Shortly thereafter, Blazor WebAssembly was released. Unlike the prototype, it used theMono .NET runtime on WebAssembly. This is the same runtime that is used for developing mobile apps with.NET MAUI (previouslyXamarin).
The Blazor source code was first located in its own repository on GitHub, until it was merged into the ASP.NET Coremonorepo. The development has been carried out from there ever since.
With the release of .NET 5, Blazor stopped working onInternet Explorer and thelegacy version ofMicrosoft Edge.[8]
In 2023, with .NET 8, Blazor on the server underwent some fundamental changes[9] to enableserver-side rendered (SSR) pages that are not fundamentally interactive, allowing Blazor to be used as an alternative toMVC 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 are formally referred to asRazor 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.
The following example shows how to implement a simple counter that can be incremented by clicking a button:
<h1>Counter</h1><p>Count: @count</p><button@onclick="Increment">Increment</button>@code { private int count = 0; private void Increment() { count++; }}
Blazor apps can be hosted in multiple ways. These are the hosting models as of .NET 8.
A server-hosted Blazor app, as part of an ASP.NET Core app.
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.
Source:[10]
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.
Interactive components are 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.
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 theDOM. That way the page doesn't flicker as it usually does when being completely reloaded upon navigating to another page.
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 aProgressive 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.
Allows Blazor apps to run within a native app using a WebView.[11] Rendering is performed in the hosting process, without a web server.
Hybrid apps can be hosted inWindows Presentation Foundation orWinForms application.
This approach is used for building native mobile apps with Blazor, using.NET MAUI.
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.