Search code examples
c#.net-coreblazorblazor-server-sidehead

Blazor PageTitle tag not working after migrating to .NET Core 6


I tried to configure the page title in my Blazor Server application but, unfortunatelly, the tag <PageTitle> not working. This project was started before Microsoft release .NET Core 6 and, after that, I migrated my application to this version.

There aren't errors in compiling or running.

Example page:

@page "/page"

<PageTitle>My page title</PageTitle>

...

Solution

  • For using the <PageTitle> tag with the new version of .NET Core, server-side and not webAssembly, you need replace your <title> tag with a new component and add this library into the file Pages/_Host.cshtml.

    @using Microsoft.AspNetCore.Components.Web
    
    <head>
      <component type="typeof(HeadOutlet)" render-mode="ServerPrerendered" />
    </head>
    

    Project scaffolding created from .NET Core 6 adds these parts automatically in Pages/_Layout.cshtml

    Theoretically, users with migrating WebAssembly projects need add too the code on below in Program.cs

    builder.RootComponents.Add<HeadOutlet>("head::after");