Toolbelt.Blazor.HeadElement
5.4.0
Head element support (change the document title, "meta" elements such as OGP, and "link" elements such as canonical url, favicon) for Blazor apps.
Install-Package Toolbelt.Blazor.HeadElement -Version 5.4.0
dotnet add package Toolbelt.Blazor.HeadElement --version 5.4.0
<PackageReference Include="Toolbelt.Blazor.HeadElement" Version="5.4.0" />
paket add Toolbelt.Blazor.HeadElement --version 5.4.0
Blazor Head Element Helper 
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 since .NET 5.0 Preview 8 also started to provide Title
, Link
, and Meta
components by Microsoft.AspNetCore.Components.Web.Extensions
NuGet package.
However, I'll continue to provide this package and keep to maintain, because this library still has some advantages.
To know what differences are there between this library and Microsoft.AspNetCore.Components.Web.Extensions 5.0 preview 8, please see the following table.
Feature | This library | .NET5 Preview 8
--------------------------------------|-------------------------|--------------------------------------------
Server Pre-Rendering | Supported. | Not Supported, yet.
Respecting pre-rendered title | Yes. | No. It is never recovered if any components override it.
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. | Not Supported.
To Including helper JavaScript | Automatic. | Required manually.
How to use
Installation
- Add package to your project like this.
dotnet add package Toolbelt.Blazor.HeadElement
- 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.
...
- Open
Toolbelt.Blazor.HeadElement
namespace in_Imports.razor
file.
@* This is "_Imports.razor" *@
...
@using Toolbelt.Blazor.HeadElement
A. Change the title of the document
- 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.
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).
C. Change "link" elements
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()}")" />
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")
);
...
E. Server-side pre-rendering support
If you want to get srever-side pre-rendering support, do this.
- Add
Toolbelt.Blazor.HeadElement.ServerPrerendering
package to your project like this.
dotnet add package Toolbelt.Blazor.HeadElement.ServerPrerendering
- Register "Head Element Server Prerendering" middleware at your server-side Blazor app's
Startup
, beforeapp.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()
...
License
Blazor Head Element Helper 
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 since .NET 5.0 Preview 8 also started to provide Title
, Link
, and Meta
components by Microsoft.AspNetCore.Components.Web.Extensions
NuGet package.
However, I'll continue to provide this package and keep to maintain, because this library still has some advantages.
To know what differences are there between this library and Microsoft.AspNetCore.Components.Web.Extensions 5.0 preview 8, please see the following table.
Feature | This library | .NET5 Preview 8
--------------------------------------|-------------------------|--------------------------------------------
Server Pre-Rendering | Supported. | Not Supported, yet.
Respecting pre-rendered title | Yes. | No. It is never recovered if any components override it.
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. | Not Supported.
To Including helper JavaScript | Automatic. | Required manually.
How to use
Installation
- Add package to your project like this.
dotnet add package Toolbelt.Blazor.HeadElement
- 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.
...
- Open
Toolbelt.Blazor.HeadElement
namespace in_Imports.razor
file.
@* This is "_Imports.razor" *@
...
@using Toolbelt.Blazor.HeadElement
A. Change the title of the document
- 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.
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).
C. Change "link" elements
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()}")" />
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")
);
...
E. Server-side pre-rendering support
If you want to get srever-side pre-rendering support, do this.
- Add
Toolbelt.Blazor.HeadElement.ServerPrerendering
package to your project like this.
dotnet add package Toolbelt.Blazor.HeadElement.ServerPrerendering
- Register "Head Element Server Prerendering" middleware at your server-side Blazor app's
Startup
, beforeapp.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()
...
License
Release Notes
v.5.4.0
- Reduce the size of the state string used for server prerendering.
Dependencies
-
.NETStandard 2.0
- Microsoft.AspNetCore.Components.Web (>= 3.0.0)
- Toolbelt.Blazor.HeadElement.Services (>= 1.5.0)
-
net5.0
- Microsoft.AspNetCore.Components.Web (>= 5.0.0)
- Toolbelt.Blazor.HeadElement.Services (>= 1.5.0)
Used By
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on Toolbelt.Blazor.HeadElement:
Repository | Stars |
---|---|
Garderoben/MudBlazor
Blazor Component Library based on Material design. The goal is to do more with Blazor, utilizing CSS and keeping Javascript to a bare minimum.
|
Version History
Version | Downloads | Last updated |
---|---|---|
5.4.0 | 339 | 1/10/2021 |
5.3.0 | 124 | 1/10/2021 |
5.2.0 | 142 | 1/10/2021 |
5.1.0 | 72 | 1/9/2021 |
5.0.0 | 4,578 | 11/10/2020 |
5.0.0-rc.1 | 851 | 9/7/2020 |
1.2.0 | 9,442 | 6/4/2020 |
1.2.0-beta | 123 | 5/29/2020 |
1.1.1 | 1,546 | 4/16/2020 |
1.1.0.1 | 1,070 | 1/18/2020 |
1.0.0 | 602 | 11/1/2019 |
0.0.1-preview2.0.2 | 108 | 9/19/2019 |
0.0.1-preview1.0.1 | 109 | 9/12/2019 |