My Favorite C# Features - Part 5: Build for the Modern Web with Blazor
Tags: dotnet, csharp, programming, blazor
Published: 2021-08-20 16:15:55

---
title: My Favorite C# Features - Part 5: Build for the Modern Web with Blazor 
published: true
description: Fritz continues his series in discussing C# features by introducing the Blazor web framework
tags: dotnet, csharp, programming, blazor
series: My Favorite C# Features
---

I've written a bit about the various language features that I really like about C# over the last few months, but today I'm going to pivot slightly and talk about how much fun it is to write web applications with the Blazor web framework.

## What is Blazor?

Blazor is a modern component-based web framework that is built on top of the .NET runtime and can run on either a web server OR in a browser using Web Assembly.  Blazor was introduced as an experiment by Steve Sanderson at NDC Oslo in 2017:

{% youtube MiLAE6HMr10 %}

This initial simple concept for building and running C# and .NET code in the browser as a "new SPA framework" captured the imaginations of .NET developers.  After 2 years of experimentation and exploration by the .NET engineering team, Blazor became an official .NET framework with Web Assembly and Server hosted versions available.

### Web Assembly AND Server version?

Blazor gives developers a way to define components and pages using C# with [Razor templates](https://docs.microsoft.com/aspnet/core/mvc/views/razor?view=aspnetcore-5.0).  What if you really like the Blazor technology, but don't want to deliver and run the content in the browser using Web Assembly?  Maybe you have some code that you need to ensure is run in a validated and protected server-based environment, well you can do that with Blazor Server-Side.

![Blazor Server-Side Framework Architecture](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tdua2abgo4lhricwdp4j.png)

In this model, Blazor pages and components are rendered on the server and delivered to the web browser using web sockets and more specifically the [SignalR framework](https://docs.microsoft.com/aspnet/core/tutorials/signalr?view=aspnetcore-5.0).  All interactions in the browser are transmitted to the server over a SignalR connection (typically an abstraction over web sockets) and the rendered HTML is returned and updated by the Blazor Server-Side framework.

## How do I get started?

Blazor is part of the .NET toolset.  You can get started by headed to https://blazor.net and installing the .NET command-line tools or Visual Studio.  I have a [new playlist of videos](https://www.youtube.com/playlist?list=PLdo4fOcmZ0oUJCA3DCzKT79Oe3kdKEceX) that will walk you through some simple scenarios that you can find on YouTube:

{% youtube 9BhbGbTsyeE %}

## Sample Page 

A simple Blazor page that can be used to fetch some data from an HTTP-based API can be queried and its contents displayed with a Blazor page like the following:

```html
@page "/fetchdata"
@inject HttpClient Http

<h1>Weather forecast</h1>

<p>This component demonstrates fetching data from the server.</p>

@if (forecasts == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <table class="table">
        <thead>
            <tr>
                <th>Date</th>
                <th>Temp. (C)</th>
                <th>Temp. (F)</th>
                <th>Summary</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var forecast in forecasts)
            {
                <tr>
                    <td>@forecast.Date.ToShortDateString()</td>
                    <td>@forecast.TemperatureC</td>
                    <td>@forecast.TemperatureF</td>
                    <td>@forecast.Summary</td>
                </tr>
            }
        </tbody>
    </table>
}

@code {
    private WeatherForecast[] forecasts;

    protected override async Task OnInitializedAsync()
    {
        forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("sample-data/weather.json");
    }
}
```

This is the basic FetchData sample that is delivered in the Blazor templates.  It shows the following techniques in building a Blazor page:

- Defining that this is a page with an address to be routed to using the `@page` directive on the first line.
- Injecting an `HttpClient` named `Http` with the `@inject` directive on the second line.  The `HttpClient` is later used to get data in JSON format and convert it to an array of `WeatherForecast` objects to be painted on screen.
- Emitting some standard table HTML tags with a for-loop across our collection of weather forecasts.  The C# code in a Razor template is denoted by the `@` character
- Handling the OnInitializedAsync event on the page to load data when the Blazor runtime initializes the page.

## Community Support and Interactions

There is a growing and enthusiastic community of Blazor developers, projects, and companies building tools and samples for Blazor.  Check out the Awesome Blazor list on GitHub to see more:

{% github AdrienTorris/awesome-blazor %}

## Summary

Building web applications with Blazor is a great way for developers who enjoy C# and .NET to create interactive web applications.  I encourage you to check out the [Blazor 101 video series](https://www.youtube.com/playlist?list=PLdo4fOcmZ0oUJCA3DCzKT79Oe3kdKEceX) published on YouTube and check out https://blazor.net for more learning materials and the tools to get started with Blazor.