Blazored SessionStorage is a library that provides access to the browsers session storage APIs for Blazor applications. An additional benefit of using this library is that it will handle serializing and deserializing values when saving or retrieving them.
From v4 onwards we use the default the JsonSerializerOptions
for System.Text.Json
instead of using custom ones. This will cause values saved to session storage with v3 to break things.
To retain the old settings use the following configuration when adding Blazored SessionStorage to the DI container:
builder.Services.AddBlazoredSessionStorage(config => {
config.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
config.JsonSerializerOptions.IgnoreNullValues = true;
config.JsonSerializerOptions.IgnoreReadOnlyProperties = true;
config.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
config.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
config.JsonSerializerOptions.ReadCommentHandling = JsonCommentHandling.Skip;
config.JsonSerializerOptions.WriteIndented = false;
}
);
Prior to v2 we bypassed the serialization of string values as it seemed pointless as string can be stored directly. However, this led to some edge cases where nullable strings were being saved as the string "null"
. Then when retrieved, instead of being null the value was "null"
. By serializing strings this issue is taken care of.
For those who wish to save raw string values, a new method SetValueAsString[Async]
is available. This will save a string value without attempting to serialize it and will throw an exception if a null string is attempted to be saved.
To install the package add the following line to you csproj file replacing x.x.x with the latest version number (found at the top of this file):
<PackageReference Include="Blazored.SessionStorage" Version="x.x.x" />
You can also install via the .NET CLI with the following command:
dotnet add package Blazored.SessionStorage
If you're using Jetbrains Rider or Visual Studio you can also install via the built in NuGet package manager.
< 8000 div class="markdown-heading" dir="auto">