Understanding .NET Blazor

Meghna
4 min readJun 7, 2024

--

Blazor

If you are working with ASP.NET Core and want to become a full-stack developer without learning a line of Javascript or Javascript frameworks, then you need to learn Blazor as soon as possible.

Blazor is a .NET framework that is used for frontend development and it supports both client-side and server-side rendering in a single solution. You can leverage your knowledge of HTML, CSS, and C# and create a beautiful full-stack web application that will handle both frontend and backend in a single application. The best part about developing a Blazor application is that you do not need to write a line of Javascript in your code, all the events can be handled by leveraging C#.

Blazor apps are based on Components. A component is a simple fundamental building block that is used to create the User Interface of an application. They are reusable, self-contained pieces of UI that can range from implementing simple elements like buttons to complex structures like forms or entire pages. Components can be reused across multiple applications, reducing code duplication. Each component has its own rendering logic, state, and event handling which makes code modular and easier to manage. They are typically written in razor syntax using HTML and C#.

The Blazor Application can be hosted using different models like WebAssembly, Server, and Hybrid Model.

Why should you use Blazor?

Okay, so we have seen a small introduction to Blazor, now we will see why should we use or learn Blazor.

  1. Using Blazor, we can create interactive and responsive UIs by using C# code.
  2. We can leverage the existing .NET ecosystem of .NET Libraries.
  3. The performance, security, and reliability that .NET provides are highly beneficial.
  4. In Blazor, the UI is quickly delivered to the browser by only rendering HTML, as it does not need to download the huge javascript bundles.
  5. Blazor supports interactive server-side rendering which is done through a live connection between browser and server, which helps in processing user actions and updates the UI in real-time.
  6. Here, the server can directly handle the user interaction like button clicks or form submissions which eliminates the need to create a separate API endpoint enabling a dynamic and interactive user experience.
  7. Initially, the server sends the HTML content without activating the event handlers. Once the initial content is loaded, the real-time connection kicks in, enabling the interactive features. This makes the page load faster and feels more responsive.

What are Blazor hosting models?

Razor components can run in server-side using ASP.NET Core versus client-side in the browser on a WebAssembly-based .NET Runtime. Blazor applications can also be hosted in hybrid mode. The same razor components can be used with any of the hosting models unchanged.

  1. Blazor Server: Here, the components are executed on the server, from within an ASP.NET Core app. All the event handling and UI updates are handled using SignalR connection using WebSockets protocol. In Blazor Server Hosting model, each browser screen represents separate instances of server-managed component states. If a browser tab is closed or any external URL is navigated to, a graceful termination occurs where all the resources are immediately released. The Blazor app takes full capabilities of server resources including .NET Core, and .NET/C# code base including the app’s component code is not served to the clients. Here, we have to deploy our code on a server so that the end user can access it and there is no offline support. If the client connection fails, interactivity fails.
  2. Blazor WebAssembly: It runs the component's client-side code in the browser on a WebAssembly-based .NET runtime. Here, the razor components their runtime, and the .NET assemblies are downloaded in the browser. Components are executed directly on a browser UI thread. Here, assets are deployed as static files to a web server or a service capable of serving static content to clients. In Blazor web assembly, a large app takes a relatively longer time to download to a browser, which diminishes the user experience. In standalone Blazor WebAssembly apps, there’s no .NET server-side dependency after the app is downloaded from the server, so the app remains functional if the server goes offline also, in standalone Blazor WebAssembly applications, serverless deployment is possible. Here, razor components are restricted to the capabilities of the browser also, capable client hardware and software are required.
  3. Blazor Hybrid: Blazor Hybrid apps can be built using different .NET native app frameworks, including .NET MAUI, WPF, and Windows Forms. Using Blazor with .NET MAUI offers a convenient way to build cross-platform Blazor Hybrid apps for mobile and desktop, while Blazor integration with WPF and Windows Forms can be a great way to modernize existing apps.

You can learn more about Blazor Hosting Models here.

So, this is some basic useful information about Blazor, which you can keep in mind while building your next Blazor Application.

--

--

Meghna

Dedicated software sorcerer conjuring elegant solutions in the language of code.