- Home
- .NET tutorials
- Blazor Server vs. Blazor WebAssembly: Four ways in which they differ
Blazor Server vs. Blazor WebAssembly: Four ways in which they differ
Published: Monday 30 November 2020
Blazor Server and Blazor WebAssembly (also known as Blazor Wasm) differ in many ways. We look at four ways on how the Blazor hosting models are different.
How The Different Blazor Hosting Models Work
It's good to have an understanding on how the different Blazor Hosting models work before looking at the differences.
Blazor Server
Blazor Server uses a standard ASP.NET Core application. Within that application, we can integrate server-side functionality, such as integrating a SQL Server database through Entity Framework.
How Blazor Server Works Between Client and Server
Blazor WebAssembly
Blazor WebAssembly uses WebAssembly. WebAssembly is supported in most modern day browsers. But, it's not supported in IE11.
How Blazor WebAssembly Works in the Browser
How The Blazor Hosting Models Differ
Now that we have an understanding on how each Blazor hosting model works, that's have a look on how they differ.
#1: Initialisation
For both examples, we are going to use the default "WeatherForecast" application that comes with Blazor to show exactly how they work.
Homepage of Weather Forecast Demo for Blazor Server
We are going to use Google Developer Tools and map the timeline of the homepage.
Blazor Server
Google Developer Tools Network Timeline on Blazor Server Startup
Looking above, Blazor Server loads in the HTML, and CSS. It also loads in a Javascript file (blazor.server.js), which initialises a connection to SignalR via websockets.
Google Developer Tools Network Timeline on Blazor Server Link Click
There is no new network activity when clicking on a link in a Blazor Server application.
Blazor WebAssembly
Google Developer Tools Network Timeline on Blazor WebAssembly Startup
When we load a Blazor WebAssembly application, it has to download all the application into the browser.
Google Developer Tools Network Timeline on Blazor WebAssembly Link Click
It has loaded in the favicon.ico icon for some reason, but that's it.
#2: Search Engines
When developing our Blazor application, we need to consider whether search engines will need to crawl our application.
Blazor Server
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>BlazorServer</title>
<base href="/" />
<link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
<link href="css/site.css" rel="stylesheet" />
<link href="BlazorServer.styles.css" rel="stylesheet" />
</head>
<body>
<!--Blazor:{"sequence":0,"type":"server","prerenderId":"4ac92f770c5545768b8a5dc2d1dbb1ba","descriptor":"CfDJ8CKP9bhtA1FDra8KT5Yqx3XxnJlhqWvM4D7YFhj9kd7ufZDfs8pArwZzSXs2mODlzP8LwITtsTx2CIwThTJoB1Wp\u002Bl6h\u002BacJst/P2rYRg2PKXX4PSf2eyqYLpM9kBW06hVCEk6l1g9z9Kl6Q6DD77DsG\u002BYKATlmXCDDJYJusRZF2A6ZvVnXuKRtMauUWb63wOEJNpuhQ2P4HldUpwrOOxJ\u002BFlR\u002BaWNdYY4L2r5CIUQResLp//lhbKIoVUm8ykICeYvZhl814IeTXPS1D54IYpA9Ynps12NrnzY2L5GrhcArrVtck2Bp6e/HDiTpTM8J9O/Sf6ONLQzfyGfZsQTUNDn5zlASdnqlokqDsigdlzSG6"}--><div class="page" b-mxoy4q7bj7><div class="sidebar" b-mxoy4q7bj7><div class="top-row pl-4 navbar navbar-dark" b-9s0xq2xzhp><a class="navbar-brand" href b-9s0xq2xzhp>BlazorServer</a>
<button class="navbar-toggler" b-9s0xq2xzhp><span class="navbar-toggler-icon" b-9s0xq2xzhp></span></button></div>
<div class="collapse" b-9s0xq2xzhp><ul class="nav flex-column" b-9s0xq2xzhp><li class="nav-item px-3" b-9s0xq2xzhp><a href="" class="nav-link active"><span class="oi oi-home" aria-hidden="true" b-9s0xq2xzhp></span> Home
</a></li>
<li class="nav-item px-3" b-9s0xq2xzhp><a href="counter" class="nav-link"><span class="oi oi-plus" aria-hidden="true" b-9s0xq2xzhp></span> Counter
</a></li>
<li class="nav-item px-3" b-9s0xq2xzhp><a href="fetchdata" class="nav-link"><span class="oi oi-list-rich" aria-hidden="true" b-9s0xq2xzhp></span> Fetch data
</a></li></ul></div></div>
<div class="main" b-mxoy4q7bj7><div class="top-row px-4" b-mxoy4q7bj7><a href="https://docs.microsoft.com/aspnet/" target="_blank" b-mxoy4q7bj7>About</a></div>
<div class="content px-4" b-mxoy4q7bj7><h1>Hello, world!</h1>
Welcome to your new app.
<div class="alert alert-secondary mt-4" role="alert"><span class="oi oi-pencil mr-2" aria-hidden="true"></span>
<strong>How is Blazor working for you?</strong>
<span class="text-nowrap">
Please take our
<a target="_blank" class="font-weight-bold" href="https://go.microsoft.com/fwlink/?linkid=2137813">brief survey</a></span>
and tell us what you think.
</div></div></div></div><!--Blazor:{"prerenderId":"4ac92f770c5545768b8a5dc2d1dbb1ba"}-->
<div id="blazor-error-ui">
An unhandled exception has occurred. See browser dev tools for details.
<a href="" class="reload">Reload</a>
<a class="dismiss">?</a>
</div>
<script src="_framework/blazor.server.js"></script>
</body>
</html>
As we can see, a Blazor Server application outputs all the HTML into the source code.
Blazor WebAssembly
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>BlazorWasm</title>
<base href="/" />
<link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />
<link href="css/app.css" rel="stylesheet" />
<link href="BlazorWasm.styles.css" rel="stylesheet" />
</head>
<body>
<div id="app">Loading...</div>
<div id="blazor-error-ui">
An unhandled error has occurred.
<a href="" class="reload">Reload</a>
<a class="dismiss">?</a>
</div>
<script src="_framework/blazor.webassembly.js"></script>
</body>
</html>
Quite a difference with Blazor WebAssembly! It only loads the template into the source code.
#3: Server-Side Functionality
If we take a look at the Program and Startup class for a Blazor Server application, it looks similar to a ASP.NET Core Web API application.
// Program.cs
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
// Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddSingleton<WeatherForecastService>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
}
And because of that, we can add server-side tools, such as Entity Framework.
// Program.cs
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
await builder.Build().RunAsync();
}
}
It's a lot more slimed down then in a Blazor Server application.
#4: Offline Support
What happens when a Blazor application goes offline?
Message When a Blazor Server App Goes Offline
But, that's not the case with Blazor WebAssembly!
Which Hosting Model to Use?
Deciding on which hosting model to use is down to your requirements.
If you want an application that is supported by search engines, or has server-side support within the application, Blazor Server is your choice.
Related tutorials
How to create the Connect 4 game in Blazor WebAssembly in one hour!
Creating the Connect 4 game using the .NET Blazor WebAssembly framework. The ASP.NET Core application uses C# & Razor components.