Web.Storage.Core 6.0.0

dotnet add package Web.Storage.Core --version 6.0.0                
NuGet\Install-Package Web.Storage.Core -Version 6.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Web.Storage.Core" Version="6.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Web.Storage.Core --version 6.0.0                
#r "nuget: Web.Storage.Core, 6.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Web.Storage.Core as a Cake Addin
#addin nuget:?package=Web.Storage.Core&version=6.0.0

// Install Web.Storage.Core as a Cake Tool
#tool nuget:?package=Web.Storage.Core&version=6.0.0                

Web Storage

This library provides access to the browser's local and session storage using javascript APIs for the Blazor applications.

Installing

  1. To directly install the package, add the below snippet to the application .csproj file
<PackageReference Include="Web.Storage.Core" Version="x.x.x" />
  1. Install via the .NET CLI
dotnet add package Web.Storage.Core
  1. Install via the built-in NuGet package manager. Click on the project dependencies and search for Web.Storage.Core.

Setup

Register the storage services with the service collection in the application Program.cs or Startup.cs (if any).

builder.Services.AddClientStorage(); //By default the service is registered as a _LocalStorage_
//builder.Services.AddClientStorage(option => option.StorageType = WebStorageType.LocalStorage); //Service is registered as a LocalStorage
//builder.Services.AddClientStorage(option => option.StorageType = WebStorageType.SessionStorage); //Service is registered as a SessionStorage

If using legacy way to configure services.

public void ConfigureServices(IServiceCollection services)
{
    services.AddClientStorage();
    //services.AddClientStorage(option => option.StorageType = WebStorageType.LocalStorage);
}

Usage (Blazor Server)

In the razor component use the below to inject storage service.

@using WebStorage

@inject IClientStorage clientStorage
  1. Setting key and value to the storage
await clientStorage.WriteAsync("Name", "John Doe");
  1. Read value from the storage by key
var keyValue = await clientStorage.ReadAsync("Name");
  1. Remove key and value from the storage
await clientStorage.RemoveAsync("Name");

Storage Demo

Local Storage Session Storage
LocalStorageSingle SessionStorageSingle

Avoid excessively fine-grained calls

Since each call involves some overhead, it can be valuable to reduce the number of calls. rolling individual JS interop calls into a single call usually only improves performance significantly if the component makes a large number of JS interop calls (see docs).

  1. Setting multiple key and value to the storage
var payload = new Dictionary<string, object> {
    { "Name", "John Doe" },
    { "Age", 35 },
    { "IsMarried", true },
    { "DateTime", DateTime.UtcNow }
}; 

await clientStorage.WriteAsync(payload);
  1. Read multiple key and value to the storage
var payload = new Dictionary<string, object> {
    { "Name", default },
    { "Age", default },
    { "IsMarried", default },
    { "DateTime", default }
};

var keyValues = await clientStorage.ReadAsync(payload);
  1. Remove multiple key and value from the storage
var keys = new[] { "Name", "Age", "IsMarried", "DateTime" };
//var keys = new List<string> { "Name", "Age", "IsMarried", "DateTime" };

await clientStorage.RemoveAsync(keys);
  1. Remove all key and value from the storage
await clientStorage.RemoveAll();

Storage Demo

Local Storage Session Storage
LocalStorageMultiple SessionStorageMultiple
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
6.0.0 217 12/18/2022