Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Control
Note
This isn't the latest version of this article. For the current release, see the .NET 11 version of this article.
Razor components can modify the HTML <head> element content of a page, including setting the page's title (<title> element) and modifying metadata (<meta> elements).
Control <head> content in a Razor component
Specify the page's title with the PageTitle component, which enables rendering an HTML <title> element to a HeadOutlet component.
Specify <head> element content with the HeadContent component, which provides content to a HeadOutlet component.
The following example sets the page's title and description using Razor.
ControlHeadContent.razor:
@page "/control-head-content"
<PageTitle>@title</PageTitle>
<h1>Control <head> Content Example</h1>
<p>
Title: @title
</p>
<p>
Description: @description
</p>
<HeadContent>
<meta name="description" content="@description">
</HeadContent>
@code {
private string description = "This description is set by the component.";
private string title = "Control <head> Content";
}
Set a page title for components via a layout
Set the page title in a layout component:
@inherits LayoutComponentBase
<PageTitle>Page Title</PageTitle>
<div class="page">
...
</div>
HeadOutlet component
The HeadOutlet component renders content provided by PageTitle and HeadContent components.
In a Blazor Web App created from the project template, the HeadOutlet component in App.razor renders <head> content:
<head>
...
<HeadOutlet />
</head>
In an app created from the Blazor WebAssembly project template, the HeadOutlet component is added to the RootComponents collection of the WebAssemblyHostBuilder in the client-side Program file:
builder.RootComponents.Add<HeadOutlet>("head::after");
When the ::after pseudo-selector is specified, the contents of the root component are appended to the existing head contents instead of replacing the content. This allows the app to retain static head content in wwwroot/index.html without having to repeat the content in the app's Razor components.
Set a default page title in a Blazor Web App
Set the page title in the App component (App.razor):
<head>
...
<HeadOutlet />
<PageTitle>Page Title</PageTitle>
</head>
Not found page title in a Blazor WebAssembly app
In Blazor apps created from the Blazor WebAssembly Standalone App project template, the NotFound component template in the App component (App.razor) sets the page title to Not found.
App.razor:
<NotFound>
<PageTitle>Not found</PageTitle>
...
</NotFound>
Related content
- Control headers in C# code at startup
- Blazor samples GitHub repository (
dotnet/blazor-samples) (how to download)
Mozilla MDN Web Docs documentation:
ASP.NET Core