Toolbelt.Blazor.HeadElement 7.2.1-preview.1

.NET 5.0 .NET Standard 2.1
This is a prerelease version of Toolbelt.Blazor.HeadElement.
There is a newer version of this package available.
See the version list below for details.
NuGet\Install-Package Toolbelt.Blazor.HeadElement -Version 7.2.1-preview.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
dotnet add package Toolbelt.Blazor.HeadElement --version 7.2.1-preview.1
<PackageReference Include="Toolbelt.Blazor.HeadElement" Version="7.2.1-preview.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Toolbelt.Blazor.HeadElement --version 7.2.1-preview.1
#r "nuget: Toolbelt.Blazor.HeadElement, 7.2.1-preview.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Toolbelt.Blazor.HeadElement as a Cake Addin
#addin nuget:?package=Toolbelt.Blazor.HeadElement&version=7.2.1-preview.1&prerelease

// Install Toolbelt.Blazor.HeadElement as a Cake Tool
#tool nuget:?package=Toolbelt.Blazor.HeadElement&version=7.2.1-preview.1&prerelease

Blazor Head Element Helper NuGet Package

Summary

This components and services allows you to change the title of document, "meta" elements such as OGP, and "link" elements such as canonical url, favicon etc. on your Blazor app.

This package supports both seiver-side Blazor and client-side Blazor WebAssembly app.

And also supports server-side pre-rendering on your server-side Blazor app.

See also following the live demonstration sites.

Notice

Blazor on .NET 6 (since preview 7) or later has started to provide <PageTitle> and <HeadContent> components.

"ASP.NET Core updates in .NET 6 Preview 7 - Modify HTML <head> content from Blazor components" | ASP.NET Blog

However, I'm going to continue to provide this package and keep maintaining, because this library still has a few advantages in some edge cases.

Please see the following table to know the differences between this library and <PageTitle>/<HeadContent> components in .NET 6 or later.

Feature This library .NET6 or later
Overriding pre-rendered meta or link 👍 Can handle it properly. 😢 Just append it. it may cause duplication.
Canceling <meta "http-equiv"="refresh" /> 👍 Works well. 😢 There is no support.
Using it as a service, not components 👍 Supported. 😢 Maybe not Supported.
Modify <head> content from many places 👍 Can do. 😢 Only one instance of the <HeadContent> component can effect.
Server-side pre-rendering 👍 Just add one line inside the server code. 😢 You have to split the fallback page into two .cshtml files for resolving complicated rendering order problems.
Controlling the order of elements inside a <head> 😢 hard 👍 Can control perfectly
Support 😢 Just personal hobby 👍 Official support from the ASP.NET Team

How to use

Installation

  1. Add package to your project like this.
dotnet add package Toolbelt.Blazor.HeadElement
  1. Register "Head Element Helper" service at your Blazor app's Startup.
using Toolbelt.Blazor.Extensions.DependencyInjection; // <- Add this, and...

public class Startup
{
  public void ConfigureServices(IServiceCollection services)
  {
    services.AddHeadElementHelper(); // <- Add this.
    ...

If the project is Blazor WebAssembly App v.3.2+, you should do it in Program class, instead.

using Toolbelt.Blazor.Extensions.DependencyInjection; // <- Add this, and...
...
public class Program
{
  public static async Task Main(string[] args)
  {
    var builder = WebAssemblyHostBuilder.CreateDefault(args);
    ...
    builder.Services.AddHeadElementHelper(); // <- Add this.
    ...
  1. Open Toolbelt.Blazor.HeadElement namespace in _Imports.razor file.
@* This is "_Imports.razor" *@
...
@using Toolbelt.Blazor.HeadElement

A. Change the title of the document

  1. Markup <Title> component in your .razor file.
@* This is "Pages/Counter.razor" *@
@page "/counter"

<Title>Counter(@currentCount) - Server Side App</Title>

The title of document will be changed.

fig1

B. Change "meta" elements

You can also add or override "meta" elements at runtime dynamically using <Meta> component like this.

@* This is "Pages/Counter.razor" *@
@page "/counter"

<Meta Property="ogp:title" Content="Counter" />

Note: You can also use native attribute names (lower and kebab case. ex: "http-equiv") instead of Razor component parameter names (pascal case).

You can add or override "link" elements at runtime dynamically using <Link> component like this.

@* This is "Pages/Counter.razor" *@
@page "/counter"

<Link Rel="icon" Href="@($"/favicons/{GetFaviconName()}")" />

fig3

Note: You can also use native attribute names (lower and kebab case) instead of Razor component parameter names (pascal case).

D. IHeadElementHelper

You can do these tasks by using IHeadElementHelper service instead of using <Title>, <Meta>, and <Link> components.

You can get the IHeadElementHelper service instnace by "Dependency Injection" mechanism.

@inject IHeadElementHelper HeadElementHelper
@using static Toolbelt.Blazor.HeadElement.MetaElement
...
@code {
  protected override async Task OnInitializedAsync()
  {
    await HeadElementHelper.SetTitleAsync("Wow!");
    await HeadElementHelper.SetMetaElementsAsync(
      ByName("description", "Foo bar..."),
      ByProp("og:title", "WoW!")
    );
    await HeadElementHelper.SetLinkElementsAsync(
      new LinkElement("canonical", "https://foo.com/bar")
    );
    ...

Server-side pre-rendering support

If you want to get srever-side pre-rendering support, do this.

  1. Add Toolbelt.Blazor.HeadElement.ServerPrerendering package to your project like this.
dotnet add package Toolbelt.Blazor.HeadElement.ServerPrerendering
  1. Register "Head Element Server Prerendering" middleware at your server-side Blazor app's Startup, before app.UseStaticFiles().
using Toolbelt.Blazor.Extensions.DependencyInjection; // <- Add this, and...
...
public class Startup
{
  ...
  public void ConfigureServices(IServiceCollection services)
  {
    services.AddHeadElementHelper(); // <!- Don't miss this line, and...
    ...

  public void Configure(IApplicationBuilder app)
  {
    app.UseHeadElementServerPrerendering(); // <- Add this.
    ...
    app.UseStaticFiles()
    ...

fig2

License

Mozilla Public License Version 2.0

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.1
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (3)

Showing the top 3 popular GitHub repositories that depend on Toolbelt.Blazor.HeadElement:

Repository Stars
jsakamoto/Toolbelt.Blazor.HeadElement
Head element support (change the document title, "meta" elements such as OGP, and "link" elements) for Blazor apps.
ADefWebserver/Blazor-Blogs
Simple blogging application written in Microsoft Server Side Blazor
jsakamoto/BlazorWasmPreRendering.Build
When you publish your Blazor Wasm app, this package pre-renders and saves the app as static HTML files in your public folder.
Version Downloads Last updated
7.2.1 130 9/18/2022
7.2.1-preview.1 57 9/8/2022
7.2.0 3,780 4/20/2022
7.1.1 293 4/12/2022
7.1.0 631 3/28/2022
7.0.0 1,974 1/23/2022
6.0.2 4,360 12/15/2021
6.0.1 1,527 11/30/2021
6.0.0 12,770 9/4/2021
6.0.0-preview.4 196 8/17/2021
6.0.0-preview.3 129 8/12/2021
6.0.0-preview.2 238 8/7/2021
6.0.0-preview.1 218 7/31/2021
5.4.3 3,287 7/30/2021
5.4.3-preview.1 266 7/10/2021
5.4.2 11,967 5/9/2021
5.4.1 13,290 1/30/2021
5.4.0 2,561 1/10/2021
5.3.0 285 1/10/2021
5.2.0 300 1/10/2021
5.1.0 267 1/9/2021
5.0.0 10,893 11/10/2020
5.0.0-rc.1 1,017 9/7/2020
1.2.0 12,681 6/4/2020
1.2.0-beta 259 5/29/2020
1.1.1 2,093 4/16/2020
1.1.0.1 1,351 1/18/2020
1.0.0 809 11/1/2019
0.0.1-preview2.0.2 239 9/19/2019
0.0.1-preview1.0.1 222 9/12/2019

v.7.2.1
- Fix: Did not work correctly on Firefox.