This little demo is based on a Gist written by Steve Sanderson where he shows how to persist state in the browser using sessionStorage. His demo shows how to persist the counter value but I wanted to try something a bit more real-world, a very simple shopping cart, and also to use localStorage rather than sessionStorage. You can download the project here.

Install Packages

Microsoft.AspNetCore.ProtectedBrowserStorage

Microsoft.AspNetCore.Components.ProtectedBrowserStorage

Note:

  • This is a .NET Core 3.1 framework will not work with .NET 5.0
  • Use Blazored.LocalStorage package instead for .NET 5.0

Sources:

http://www.appvnext.com/blog/2020/3/7/blazor-shopping-cart-sample-using-local-storage-to-persist-state

https://gist.github.com/SteveSandersonMS/ba16f6bb6934842d78c89ab5314f4b56

Last modified: April 1, 2021

Author

Comments

Write a Reply or Comment