Can i make Sessionstorage in Blazor Web App
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jayraj ChhayaRank #304 · 8.2K reputationPosted Sep 26, 2024, 7:13 AM EDT
Yes, you can implement session storage in a Blazor Web App. Blazor provides a way to interact with the browser's session storage through JavaScript interop. This allows you to store data that persists for the duration of the page session.
To achieve this, you can create a JavaScript file that contains functions to set, get, and remove items from session storage.
// sessionStorage.js function setSessionItem(key, value) { sessionStorage.setItem(key, value); } function getSessionItem(key) { return sessionStorage.getItem(key); } function removeSessionItem(key) { sessionStorage.removeItem(key); }Next, you can call these functions from your Blazor components using the
IJSRuntimeservice:@inject IJSRuntime JSRuntime Save Data Load Data @code { private async Task SaveData() { await JSRuntime.InvokeVoidAsync("setSessionItem", "myKey", "myValue"); } private async Task LoadData() { var value = await JSRuntime.InvokeAsync("getSessionItem", "myKey"); Console.WriteLine(value); } }