From 85dd6009e162cf834e9552f28191b8498b988a3e Mon Sep 17 00:00:00 2001 From: Luke Latham <1622880+guardrex@users.noreply.github.com> Date: Fri, 6 Mar 2026 17:23:48 -0500 Subject: [PATCH] Add protected browser storage example (#36846) --- .../protected-browser-storage.md | 47 ++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/aspnetcore/blazor/state-management/protected-browser-storage.md b/aspnetcore/blazor/state-management/protected-browser-storage.md index d97f7b4f793d..07aead74517a 100644 --- a/aspnetcore/blazor/state-management/protected-browser-storage.md +++ b/aspnetcore/blazor/state-management/protected-browser-storage.md @@ -224,7 +224,52 @@ When prerendering is disabled, [prerendering of `
` content](xref:blazor/co Prerendering might be useful for other pages that don't use `localStorage` or `sessionStorage`. To retain prerendering, defer the loading operation until the browser is connected to the circuit. The following is an example for storing a counter value: -:::moniker range=">= aspnetcore-5.0" +:::moniker range=">= aspnetcore-9.0" + +```razor +@using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage +@inject ProtectedLocalStorage ProtectedLocalStore + +@if (RendererInfo.IsInteractive) +{ +Current count: @currentCount
+ +} +else +{ +Loading...
+} + +@code { + private int currentCount; + + protected override async Task OnInitializedAsync() + { + if (RendererInfo.IsInteractive) + { + await LoadStateAsync(); + } + } + + private async Task LoadStateAsync() + { + var result = await ProtectedLocalStore.GetAsync