Managing State (Part 3)
Implementing WebAssembly
We will use
Blazored.SessionStorage for Blazor WebAssembly:
- Right-click on the Dependencies node under the MyBlogWebAssembly.Client project and select Manage Nuget Package.
- Search for Blazored.SessionStorage and click Install.
- Right-click on the MyBlogWebAssembly.Client project and select Add|New Folder. Name the folder Services.
- Select the new folder and
press Shift+ F2. Name the file MyBlogBrowserStorage.cs.
- Open the new file and replace the content with code.
- In the Program.cs file, add the following namespaces-
using
Blazored.SessionStorage;
using
MyBlog.Shared.Interfaces;
using
MyBlogWebAssembly.Client.Services;
- Add code just above await
builder.Build().RunAsync();
Then we add our configuration for IBrowserStorage,
but in this case, we return MyBlogBrowserStorage when we ask the
dependency injection for IBrowserStorage.
Implementing the shared
We also need to implement
some code that calls the services we just created:
- In the MyBlog.Shared project, open Pages/Admin/BlogPostEdit.razor. We are going to make a couple of changes to the file.
- Inject IBrowserStorage-
@inject
MyBlog.Shared.Interfaces.IBrowserStorage storage
- In the OnParameterSetAsync method, we load post if Id is not null. Also add an else clause to the if statement. When we load a file and Id is null, this means we are editing a new file and then we can check whether we have a file saved in browser storage.
- We need UpdateHTML method to become async. If Id on the blog post is 0 (zero), we will store the post in the browser storage. Make sure to change all the references from UpdateHTML to UpdateHTMLAsync.
- In the MyBlog.Data.Shared project in the Models/BlogPost.cs file, instantiate the Tags collection. like this-
- public ICollection <Tag> Tags { get; set; } = new Collection <Tag> ();
Now it's time to test the
implementation:
- Right-click on MyBlogServerSide, select Set as Startup Project, and run the project by pressing Ctrl+ F5.
- Log in to the site (so we can access the admin tools).
- Click Blog posts followed by New blog post.
- Type anything in the boxes, and as soon as we type something in the text area, it will save the post to storage.
- Click Blog posts (so we navigate away from our blog post).
- Click New blog post and all the information will still be there.
- Press F12 to see the browser developer tools. Click Application | Session storage | https://localhost:5000.
You should see one post
with the key EditCurrentPost, and the value of that post should be an
encrypted string.
Let's test the Blazor
WebAssembly next:
- Right-click on MyBlogWebAssembly.Server, select Set as Startup Project, and run the project by pressing Ctrl+ F5.
- Log in to the site (so we can access the admin tools).
- Click Blog posts and then New blog post.
- Type anything in the boxes, and as soon as we type something in the text area, it will save the post to storage.
- Click Blog posts (so we navigate away from our blog post).
- Click New blog post and all the information should still be there.
- Press F12 to see the browser developer tools. Click Application | Session storage | https://localhost:5000.
You should see one post
with the key EditCurrentPost, and the value of that post should be a
JSON string. If we were to change the data in the storage, it would also change
in the application, so keep in mind that this is plain text, and the end user
can manipulate the data.
Conclusion
We have successfully implemented
as well as protected browser storage for Blazor Server and session storage for
Blazor WebAssembly.
Comments
Post a Comment