Blazor Extensions are a set of packages with the goal of adding useful things to Blazor.
This package wraps HTML5 Storage APIs. Both Session and Local storage types are supported.
The following snippet shows how to setup the storage wrapper by registering it for dependency injection in the Startup.cs of the application.
publicvoidConfigureServices(IServiceCollectionservices){// Add Blazor.Extensions.Storage// Both ISessionStorage and ILocalStorage are registeredservices.AddStorage();}The following snippet shows how to consume the storage API in a Blazor component.
@injectISessionStorage sessionStorage
@inject ILocalStorage localStorage
@functions {protectedoverrideasyncTaskOnInitAsync(){varkey="forecasts";awaitsessionStorage.SetItem<WeatherForecast[]>(key,forecasts);awaitlocalStorage.SetItem<WeatherForecast[]>(key,forecasts);varfromSession=awaitsessionStorage.GetItem<WeatherForecast[]>(key);varfromLocal=awaitlocalStorage.GetItem<WeatherForecast[]>(key);}}If you want to consume it outside of a cshtml based component, then you can use the Inject attribute to inject it into the class.
[Inject]protectedISessionStoragesessionStorage;[Inject]protectedILocalStoragelocalStorage;
public Task LogSomething(){varkey="forecasts";awaitsessionStorage.SetItem<WeatherForecast[]>(key,forecasts);awaitlocalStorage.SetItem<WeatherForecast[]>(key,forecasts);varfromSession=awaitsessionStorage.GetItem<WeatherForecast[]>(key);varfromLocal=awaitlocalStorage.GetItem<WeatherForecast[]>(key);}Please feel free to use the component, open issues, fix bugs or provide feedback.
The following people are the maintainers of the Blazor Extensions projects: