Improve the PersistentComponentStateSerializer example#37184
Merged
guardrex merged 6 commits intoMay 22, 2026
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Updates the Blazor prerendered state persistence article to provide a more complete, copy/paste-ready example of implementing a custom PersistentComponentStateSerializer<T>.
Changes:
- Replaces the prior generic custom-serializer description with a concrete
int?custom serializer example (CustomIntSerializer) that demonstrates non-JSON serialization. - Updates the DI registration snippet to show registering the custom serializer for
int?. - Adds an example logging output section intended to show how the serializer is invoked during prerendering/restore.
Comments suppressed due to low confidence (1)
aspnetcore/blazor/state-management/prerendered-state-persistence.md:377
Restorereturns0when parsing fails, which silently changes data and is especially problematic for anint?serializer (it can't distinguish invalid data from a real 0). It should returnnull(or otherwise preserve the absence of a value) when the custom payload isnull/empty or cannot be parsed.
if (text.StartsWith("CUSTOM:", StringComparison.Ordinal) &&
int.TryParse(text.AsSpan(7), out int value))
{
return value;
}
// Fallback to direct parsing if format is unexpected
return int.TryParse(text, out int fallbackValue) ? fallbackValue : 0;
}
1 task
wadepickett
reviewed
May 22, 2026
wadepickett
approved these changes
May 22, 2026
Contributor
wadepickett
left a comment
There was a problem hiding this comment.
Approved. Looks great. I don't see any issues in the PR additions.
I did make a suggestion which you could keep or not for the intro of the article.
…ce.md Co-authored-by: Wade Pickett <wpickett@microsoft.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #37181
Thanks, @htmlsplash! 🚀
This PR improves the
PersistentComponentStateSerializerexample.Tom, Wade ... Just need one review on this one, and we don't need to bother Javier. It's taken directly from his framework test at ...
https://github.com/dotnet/aspnetcore/blob/main/src/Components/test/testassets/TestContentPackage/CustomIntSerializer.cs
... with updates to make it for nullable integers (
int?) and works great as a fully working, cut-'n-paste demo! 🎉Note that Copilot suggested the addition of ...
... which I did add. However, those are in the shared framework. In an app, these namespaces aren't required, but let's go ahead ... no harm ... and the dev can knock them out. If you think the section should remark on this, I can add a sentence or two stating that these namespaces are in the shared framework. IMO, it's probably not necessary.
Internal previews