Persist Component State Tag Helper in ASP.NET Core

The Persist Component State Tag Helper saves the state of non-routable Razor components rendered in a page or view of a Razor Pages or MVC app.

Prerequisites

Follow the guidance in the Use non-routable components in pages or views section of the Integrate ASP.NET Core Razor components into ASP.NET Core apps article.

Follow the guidance in the Configuration section for either:

Persist state for prerendered components

To persist state for prerendered components, use the Persist Component State Tag Helper (reference source). Add the Tag Helper's tag, <persist-component-state />, inside the closing </body> tag of the layout in an app that prerenders components.

Note

Documentation links to .NET reference source usually load the repository's default branch, which represents the current development for the next release of .NET. To select a tag for a specific release, use the Switch branches or tags dropdown list. For more information, see How to select a version tag of ASP.NET Core source code (dotnet/AspNetCore.Docs #26205).

In Pages/Shared/_Layout.cshtml for embedded components that are either WebAssembly prerendered (WebAssemblyPrerendered) or server prerendered (ServerPrerendered):

<body>
    ...

    <persist-component-state />
</body>

Decide what state to persist using the PersistentComponentState service. PersistentComponentState.RegisterOnPersisting registers a callback to persist the component state before the app is paused. The state is retrieved when the application resumes.

For more information and examples, see Prerender ASP.NET Core Razor components.

To persist state for prerendered components, use the Persist Component State Tag Helper (reference source). Add the Tag Helper's tag, <persist-component-state />, inside the closing </body> tag of the _Host page in an app that prerenders components.

Note

Documentation links to .NET reference source usually load the repository's default branch, which represents the current development for the next release of .NET. To select a tag for a specific release, use the Switch branches or tags dropdown list. For more information, see How to select a version tag of ASP.NET Core source code (dotnet/AspNetCore.Docs #26205).

In Pages/_Host.cshtml of Blazor apps that are either WebAssembly prerendered (WebAssemblyPrerendered) in a hosted Blazor WebAssembly app or ServerPrerendered in a Blazor Server app:

<body>
    ...

    <persist-component-state />
</body>

Decide what state to persist using the PersistentComponentState service. PersistentComponentState.RegisterOnPersisting registers a callback to persist the component state before the app is paused. The state is retrieved when the application resumes.

For more information and examples, see Prerender and integrate ASP.NET Core Razor components.

Additional resources