Persilsoft.WebStorage.Blazor 1.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package Persilsoft.WebStorage.Blazor --version 1.0.2                
NuGet\Install-Package Persilsoft.WebStorage.Blazor -Version 1.0.2                
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="Persilsoft.WebStorage.Blazor" Version="1.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Persilsoft.WebStorage.Blazor --version 1.0.2                
#r "nuget: Persilsoft.WebStorage.Blazor, 1.0.2"                
#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 Persilsoft.WebStorage.Blazor as a Cake Addin
#addin nuget:?package=Persilsoft.WebStorage.Blazor&version=1.0.2

// Install Persilsoft.WebStorage.Blazor as a Cake Tool
#tool nuget:?package=Persilsoft.WebStorage.Blazor&version=1.0.2                

Persilsoft.WebStorage.Blazor

Provides services to interoperate with the localStorage and sessionStorage objects.


Example

First, you need to register the following group of services in the dependency container:

using ServiceCollectionExtensions;

builder.Services.AddWebStorage();

Next, you can inject the LocalStorage or SessionStorage service into you Blazor components.
The following example demonstrates how to store simple text strings in the localStorage.

@page "/localstorage-string"
@inject LocalStorage Storage

<h1>Storage - string</h1>
<hr />

<div class="row">
    <div class="offset-md-3 col-12 col-md-6">
        <div class="input-group mb-2">
            <input @bind=key type="text" class="form-control" placeholder="Your key" />
            <input @bind=value type="text" class="form-control" placeholder="Your value" />
            <button class="btn btn-primary" @onclick=Add>Add</button>
        </div>
    </div>
</div>

<table class="table">
    <thead>
        <tr>
            <th>Key</th>
            <th>Value</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in values)
        {
            <tr>
                <td>@item.Key</td>
                <td>@item.Value</td>
                <td>
                    <button class="btn btn-outline-danger" type="button" @onclick="@(() => Remove(item.Key))">
                        <i class="bi bi-trash3"></i>
                    </button>
                </td>
            </tr>
        }
    </tbody>
</table>
<div class="text-center">
    <button class="btn btn-danger" @onclick=Clear>Remove all</button>
</div>

@code {
    private string key;
    private string value;
    private Dictionary<string, string> values = new();

    protected override async Task OnInitializedAsync()
    {
        await PrintValues();
    }

    private async Task Add()
    {
        if (!string.IsNullOrWhiteSpace(key) && !string.IsNullOrWhiteSpace(value))
        {
            await Storage.SetItemAsStringAsync(key, value);
            await PrintValues();

            key = "";
            value = "";
        }
    }

    private async Task PrintValues()
    {
        values.Clear();
        int length = await Storage.CountAsync();
        for (int i = 0; i < length; i++)
        {
            string key = await Storage.GetKeyAsync(i);
            string value = await Storage.GetItemAsStringAsync(key);
            values.Add(key, value);
        }
    }

    private async Task Remove(string key)
    {
        await Storage.RemoveItemAsync(key);
        await PrintValues();
    }

    public async Task Clear()
    {
        await Storage.ClearAsync();
        await PrintValues();
    }
}

But you can also store complex types; the library will serialize objects into JSON format and store them as strings.

@page "/localstorage-complex"
@inject LocalStorage Storage

<h1>Storage - data complex</h1>
<hr />

<div class="row mb-2">
    <div class="col">
        <input @bind=person.FirstName class="form-control" placeholder="First name" />
    </div>

    <div class="col">
        <input @bind=person.LastName class="form-control" placeholder="Last name" />
    </div>
    <div class="col">
        <input @bind=person.Age class="form-control" placeholder="Age" />
    </div>
</div>
<div class="text-center">
    <button class="btn btn-primary" @onclick=Add>Add</button>
</div>

<table class="table">
    <thead>
        <tr>
            <th>First name</th>
            <th>Last name</th>
            <th>Age</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var person in persons)
        {
            <tr>
                <td>@person.FirstName</td>
                <td>@person.LastName</td>
                <td>@person.Age</td>
                <td>
                    <button class="btn btn-outline-danger" type="button" @onclick="@(() => Remove(person.Id))">
                        <i class="bi bi-trash3"></i>
                    </button>
                </td>
            </tr>
        }
    </tbody>
</table>
<div class="text-center">
    <button class="btn btn-danger" @onclick=Clear>Remove all</button>
</div>

@code {
    private Person person = new();
    private List<Person> persons;

    protected override async Task OnInitializedAsync()
    {
        await PrintValues();
    }

    private async Task Add()
    {
        if (!string.IsNullOrWhiteSpace(person.FirstName) && !string.IsNullOrWhiteSpace(person.LastName))
        {
            string newId = Guid.NewGuid().ToString();
            person.Id = newId;
            await Storage.SetItemAsync(newId, person);
            await PrintValues();
            person = new Person();
        }
    }

    private async Task PrintValues()
    {
        persons = new List<Person>();
        int length = await Storage.CountAsync();
        for (int i = 0; i < length; i++)
        {
            string key = await Storage.GetKeyAsync(i);
            var item = await Storage.GetItemAsync<Person>(key);
            persons.Add(item);
        }
    }

    private async Task Remove(string key)
    {
        await Storage.RemoveItemAsync(key);
        await PrintValues();
    }

    public async Task Clear()
    {
        await Storage.ClearAsync();
        await PrintValues();
    }

    class Person
    {
        public string Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
    }
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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
1.0.4 80 10/16/2024
1.0.3 109 7/20/2024
1.0.2 98 5/22/2024
1.0.1 121 4/23/2024
1.0.0 117 4/23/2024