Managing State (Part 2)
Implementing Browser Storage
The web browser offers
various methods for storing data, each with different handling based on the
type utilized. Local storage is limited to the specific browser window
being used by the individual. Even if the user refreshes the page or exits the
web browser entirely, the data will remain intact.
The information is also
accessible across different tabs. Session storage is limited to the
specific browser tab. If you refresh the tab, the data remains, but closing the
tab will result in loss of the data. SessionsStorage can be considered
safer because it minimizes the potential issues that could arise from multiple
tabs altering the same stored values.
To be able to access the
browser storage, we need to use JavaScript. In .NET 5, Microsoft rolled out Protected
browser storage, leveraging data protection features in ASP.NET Core,
although it is not accessible in WebAssembly. Nevertheless, we can utilize an
open source library named Blazored.LocalStorage, which is compatible
with both Blazor Server and Blazor WebAssembly.
Creating An Interface
First, we need an
interface that can read and write to storage:
- In the MyBlog.Shared project, right-click on the project name and select Add|New Folder. Name the folder Interfaces.
- Select the new folder and create a new class by pressing Shift+ F2, and name the file IBrowserStorage.cs.
- Replace the content in the file with a code.
Now we have an interface
containing get, set, and delete methods.
Implementing Blazor
Server
For Blazor Server, we
will use protected browser storage:
- Right-click on the MyBlogServerSide project and select Add|New folder. Name the folder Services.
- Select the folder and press Shift+ F2. Name the file MyBlogProtectedBrowserStorage.cs.
- Open the new file and add the following using statements-
using
Microsoft.AspNetCore.Components.Server.
ProtectedBrowserStorage;
using
MyBlog.Shared.Interfaces;
using
System.Threading.Tasks;
- Replace the class.
- In Startup.cs, add the following namespaces-
using
MyBlog.Shared.Interfaces;
using
MyBlogServerSide.Services;
- Add the following at the bottom of the ConfigureServices method-
services.AddScoped<IBrowserStorage
, MyBlogProtectedBrowserStorage> () ;
- Protected browser storage will use JavaScript to get the information, and we can only do those calls from OnAfterRenderAsync or OnAfterRender, but there is another way. The reason for JavaScript not working in places other than the OnAfterRender methods is the pre-rendering feature of Blazor Server.
- Open Pages/_host.chtml and change the render mode from <component type= “typeof (App)” render-mode= “ServerPrerendered” /> to <component type= “typeof (App)” render-mode= “Server” />. This will make it possible for us to call JavaScript outside of the OnAfterRender methods.
We are configuring Blazor to return an instance of MyBlogProtectedBrowserStorage when we inject IBrowserStorage.
Conclusion
We have successfully
implemented Blazor servers and Browser storage.
Comments
Post a Comment