- Home
- .NET tutorials
- Intro to Blazor and creating a Blazor app from scratch
Intro to Blazor and creating a Blazor app from scratch
Published: Monday 3 February 2020
Last month, Microsoft ran a .NET Conf, focusing on Blazor. Now, Blazor has been around since 2018, but to-date, it is still in preview mode. It is a web framework that uses C#, Razor and HTML and allows you to run a Single-Page Application (SPA).
Create a Web Application
Open up Visual Studio 2019 and create a new project. Now, VS 2019 should come with a Blazor template. Do a search for it in the search bar provided.
Now, you can go ahead and click on the Blazor template. VS 2019 will create a sample Blazor app for you that you can go ahead and configure.
Startup Configurations
You will need to make some changes to your Startup.cs file. Now, Blazor uses code from the "Microsoft.AspNetCore.Components" assembly to make it run. As ASP.NET Core web projects use the "Microsoft.NET.Sdk.Web" SDK assembly (you should be able to see this in your .csproj file) to run, the "Microsoft.AspNetCore.Components" assembly should already be included. So no need to go off to Nuget and install these assemblies separately.
// Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace RoundTheCode.Blazor
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages(); // Configuration for Blazor
services.AddServerSideBlazor(); // Configuration for Blazor
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseStaticFiles();
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub(); // Configuration for Blazor
endpoints.MapFallbackToPage("/_Host"); // Configuration for Blazor
});
}
}
}
Add RootAssembly to Your .csproj File
It will make your life a lot easier if you add the RootAssembly tag to your .csproj file. In your project, right-click on the project and select "Edit Project File". This will open up your XML .csproj file and you just need to make sure that it contains the RootAssembly tag. The RootAssembly tag will contain the same name as your project name:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>RoundTheCode.Blazor</RootNamespace>
</PropertyGroup>
</Project>
Create the "Razor" Part of the App
With the configuration out the way, we can now integrate Blazor properly. This is mainly done by creating a number of razor files.
The Layout
The first thing we need to do is to create a layout for our Blazor application.
Construct your Layout.razor file with the following code:
<!-- Layout.razor -->
@inherits LayoutComponentBase
<div class="main">
@Body
</div>
With your layout file, you need to inherit the "LayoutComponentBase" class. This comes from the "Microsoft.AspNetCore.Components" assembly and calling the @Body method will basically render different content depending on which page you are on. It's a bit like the @RenderBody() method used in MVC.
The App
Next, you need to create the app for Blazor. Go into the root of your project and create a Razor component. Call it App.razor.
<!-- App.razor -->
@using Microsoft.AspNetCore.Components.Routing
@using RoundTheCode.Blazor.Shared
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(Layout)" />
</Found>
<NotFound>Cannot find anything on this page</NotFound>
</Router>
The Pages
Now, we can go ahead and create the pages for our Blazor app. In our Startup.cs file, we've added the following line:
endpoints.MapFallbackToPage("/_Host");
We now need to go ahead and create the page. Create a folder called "Pages", and then add a new item. This time, rather than creating a Razor component, we need to create a Razor Page:
Call it _Host.cshtml and click add. We will then add the following code:
<!-- _Host.cshtml -->
@page "/"
@namespace RoundTheCode.Blazor.Pages
@model _HostModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
}
<html>
<head>
</head>
<body>
<app>
@(await Html.RenderComponentAsync<App>(RenderMode.ServerPrerendered))
</app>
<script src="_framework/blazor.server.js"></script>
</body>
</html>
This file basically sets up the SPA. It provides us with our base HTML and where about's in our code we want the Blazor app to run. You may notice that we are calling a reference to "blazor.server.js". As stated earlier, this file allows for communication between the client and the server. This file should already be bundled into the framework.
<!-- Index.razor -->
@using Microsoft.AspNetCore.Components.Routing
@page "/"
<h1>Hello, this is the homepage</h1>
<p>Go to the <NavLink href="/contact">Contact</NavLink> page</p>
<!-- Contact.razor -->
@page "/contact"
<h1>Contact Page</h1>
<p>This is the contact page</p>
You may notice that a @page reference to these files. This basically dictates the URL of when this content is rendered.
Run your Application
Now you have set up your Blazor application and have learnt how to do it. Run your application and you should be greeted with a page similar to this:
This is rendering Index.razor as we specified "/" in the @page reference at the top of this page. We included a link to the Contact page, which when clicked will direct you to Contact.razor.
Conclusion
That should get you going with your Blazor app. One thing that Blazor can do that other SPA's struggle with is being able to render these pages server-side. That makes it a lot easier for search engines to read the content of your pages. When running the application, view the source and you will see that the content inside your Blazor app has been rendered.
Related tutorials
Blazor Server vs. Blazor WebAssembly: Four ways in which they differ
Blazor Server versus WebAssembly (Wasm). Four ways on how the Blazor hosting models differ, including performance & offline support.