Blazored.LocalStorage 2.0.1

A library to provide access to local storage in Blazor applications

There is a newer version of this package available.
See the version list below for details.
Install-Package Blazored.LocalStorage -Version 2.0.1
dotnet add package Blazored.LocalStorage --version 2.0.1
<PackageReference Include="Blazored.LocalStorage" Version="2.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Blazored.LocalStorage --version 2.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Blazored LocalStorage

A library to provide access to local storage in Blazor applications

Important Notice For Server-side Blazor Apps

There is currently an issue with Server-side Blazor apps (not Client-side Blazor). They are unable to import static assets from component libraries such as this one.

You can still use this package, however, you will need to manually add the JavaScript file to your Server-side Blazor projects wwwroot folder. Then you will need to reference it in your index.html.

Alternatively, there is a great package by Mister Magoo which offers a solution to this problem without having to manually copy files.

Installing

You can install from Nuget using the following command:

Install-Package Blazored.LocalStorage

Or via the Visual Studio package manger.

Setup

First, you will need to register local storage with the service collection in your startup.cs file

public void ConfigureServices(IServiceCollection services)
{
    services.AddBlazoredLocalStorage();
}

Usage (Client-side Blazor)

To use Blazored.LocalStorage in client-side Blazor, inject the ILocalStorageService per the example below.

@inject Blazored.LocalStorage.ILocalStorageService localStorage

@code {

    protected override async Task OnInitAsync()
    {
        await localStorage.SetItemAsync("name", "John Smith");
        var name = await localStorage.GetItemAsync<string>("name");
    }

}

With client-side Blazor you also have the option of a synchronous API, if your use case requires it. You can swap the ILocalStorageService for ISyncStorageService which allows you to avoid use of async/await. For either interface, the method names are the same.

@inject Blazored.LocalStorage.ISyncStorageService localStorage

@code {

    protected override void OnInit()
    {
        localStorage.SetItem("name", "John Smith");
        var name = localStorage.GetItem<string>("name");
    }

}

Usage (Server-side Blazor)

NOTE: Due to pre-rendering in server-side Blazor you can't perform any JS interop until the OnAfterRender lifecycle method.

@inject Blazored.LocalStorage.ILocalStorageService localStorage

@code {

    protected override async Task OnAfterRenderAsync()
    {
        await localStorage.SetItemAsync("name", "John Smith");
        var name = await localStorage.GetItemAsync<string>("name");
    }

}

The APIs available are:

  • asynchronous via ILocalStorageService:
    • SetItemAsync()
    • GetItemAsync()
    • RemoveItemAsync()
    • ClearAsync()
    • LengthAsync()
    • KeyAsync()
  • synchronous via ISyncStorageService:
    • SetItem()
    • GetItem()
    • RemoveItem()
    • Clear()
    • Length()
    • Key()

Note: Blazored.LocalStorage methods will handle the serialisation and de-serialisation of the data for you.

Blazored LocalStorage

A library to provide access to local storage in Blazor applications

Important Notice For Server-side Blazor Apps

There is currently an issue with Server-side Blazor apps (not Client-side Blazor). They are unable to import static assets from component libraries such as this one.

You can still use this package, however, you will need to manually add the JavaScript file to your Server-side Blazor projects wwwroot folder. Then you will need to reference it in your index.html.

Alternatively, there is a great package by Mister Magoo which offers a solution to this problem without having to manually copy files.

Installing

You can install from Nuget using the following command:

Install-Package Blazored.LocalStorage

Or via the Visual Studio package manger.

Setup

First, you will need to register local storage with the service collection in your startup.cs file

public void ConfigureServices(IServiceCollection services)
{
    services.AddBlazoredLocalStorage();
}

Usage (Client-side Blazor)

To use Blazored.LocalStorage in client-side Blazor, inject the ILocalStorageService per the example below.

@inject Blazored.LocalStorage.ILocalStorageService localStorage

@code {

    protected override async Task OnInitAsync()
    {
        await localStorage.SetItemAsync("name", "John Smith");
        var name = await localStorage.GetItemAsync<string>("name");
    }

}

With client-side Blazor you also have the option of a synchronous API, if your use case requires it. You can swap the ILocalStorageService for ISyncStorageService which allows you to avoid use of async/await. For either interface, the method names are the same.

@inject Blazored.LocalStorage.ISyncStorageService localStorage

@code {

    protected override void OnInit()
    {
        localStorage.SetItem("name", "John Smith");
        var name = localStorage.GetItem<string>("name");
    }

}

Usage (Server-side Blazor)

NOTE: Due to pre-rendering in server-side Blazor you can't perform any JS interop until the OnAfterRender lifecycle method.

@inject Blazored.LocalStorage.ILocalStorageService localStorage

@code {

    protected override async Task OnAfterRenderAsync()
    {
        await localStorage.SetItemAsync("name", "John Smith");
        var name = await localStorage.GetItemAsync<string>("name");
    }

}

The APIs available are:

  • asynchronous via ILocalStorageService:
    • SetItemAsync()
    • GetItemAsync()
    • RemoveItemAsync()
    • ClearAsync()
    • LengthAsync()
    • KeyAsync()
  • synchronous via ISyncStorageService:
    • SetItem()
    • GetItem()
    • RemoveItem()
    • Clear()
    • Length()
    • Key()

Note: Blazored.LocalStorage methods will handle the serialisation and de-serialisation of the data for you.

NuGet packages (16)

Showing the top 5 NuGet packages that depend on Blazored.LocalStorage:

Package Downloads
RapidCMS.Repositories
Example repositories for RapidCMS. RapidCMS is a Blazor framework which allows you to build a responsive and flexible CMS purely from code.
MComponents
Another blazor component library
Necnat.Client
Package Description
Bracketcore.Sket
Package Description
FlareTables
Package Description

GitHub repositories (4)

Showing the top 4 popular GitHub repositories that depend on Blazored.LocalStorage:

Repository Stars
dotnet-architecture/eShopOnWeb
Sample ASP.NET Core 3.1 reference application, powered by Microsoft, demonstrating a layered application architecture with monolithic deployment model. Download the eBook PDF from docs folder.
CuriousDrive/BookStores
Getting started templates for ASP.NET Core Web API + EF Core back end
chrissainty/AuthenticationWithClientSideBlazor
Companion code sample for my blog post - Authentication with client-side Blazor using WebAPI and ASP.NET Core Identity
ThomasBleijendaal/RapidCMS
RapidCMS is a Blazor framework which allows you to build a responsive and flexible CMS purely from code. It provides a basic set of editors and controls, and is fully customisable.

Version History

Version Downloads Last updated
3.0.0 80,650 7/23/2020
2.2.0 587 7/23/2020
2.1.6 64,672 5/21/2020
2.1.5 38,248 3/31/2020
2.1.4 388 3/30/2020
2.1.3 2,080 3/27/2020
2.1.1 77,985 9/25/2019
2.1.0 1,601 9/19/2019
2.0.14 305 9/17/2019
2.0.13 781 9/5/2019
2.0.12 1,593 8/19/2019
2.0.11 577 8/14/2019
2.0.10 156 8/14/2019
2.0.9 144 8/14/2019
2.0.8 156 8/13/2019
2.0.7 765 7/27/2019
2.0.6 175 7/25/2019
2.0.5 180 7/25/2019
2.0.3 138 7/25/2019
2.0.2 925 6/13/2019
2.0.1 399 5/31/2019
2.0.0 1,058 4/19/2019
1.2.1 592 3/15/2019
1.2.0 247 3/13/2019
1.1.0 293 3/9/2019
1.0.4 235 3/9/2019
1.0.3 317 3/4/2019
1.0.2 238 3/2/2019
1.0.1 438 2/9/2019
1.0.0 289 1/30/2019
Show less