Skip to content
A library to provide access to local storage in Blazor applications
Branch: master
Clone or download
Latest commit 43a732a Mar 15, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
samples/BlazorSample
src/Blazored.LocalStorage bumped version Mar 15, 2019
.gitignore
Blazored.LocalStorage.sln
LICENSE Initial commit Jan 29, 2019
README.md Update readme Mar 9, 2019
azure-pipelines.yml

README.md

Blazored LocalStorage

A library to provide access to local storage in Blazor applications

Build Status

Nuget

Important Notice For ASP.NET Core Razor Components Apps

There is currently an issue with ASP.NET Core Razor Components apps (not 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 Razor Components 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

This is an example of using local storage in a .cshtml file

@inject Blazored.LocalStorage.ILocalStorageService localStorage

@functions {

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

}

The APIs available are

  • SetItem()
  • GetItem()
  • RemoveItem()
  • Clear()
  • Length()
  • Key()

All APIs in ILocalStorageService are async

If you are using Blazor (not Razor Components), you can choose to instead inject Blazored.LocalStorage.ISyncStorageService to opt into a synchronous API that allows you to avoid use of async/await. For either interface, the method names are the same.

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

You can’t perform that action at this time.