-
Notifications
You must be signed in to change notification settings - Fork 10.5k
TempData support for Blazor #64749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
dariatiurina
wants to merge
20
commits into
dotnet:main
Choose a base branch
from
dariatiurina:49683-tempdata
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
TempData support for Blazor #64749
+1,250
−0
Conversation
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
…etcore into 49683-tempdata
73c510a to
6002ab1
Compare
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.
TempData support for Blazor
Summary
Right now Blazor does not support TempData or similar systems, like there is support for such in MVC. This PR fixes this gap and gives users native support for persisting data between requests in Blazor SSR.
Changes
Summary of changes:
ITempDatainterface defining methods for storing, retrieving, peeking, and retaining temporary data, as well as indexer access.TempDataclass, providing the logic for managing temporary data, including methods for getting, peeking, keeping, saving, and loading data.TempDatainto the service collection, making it available as a cascading value in Blazor endpoint scenarios. It loadsTempDatafrom theHttpContextand ensures it is saved back to cookies on response.TempDataServicefor serializing/deserializingTempDatato/from cookies, handling supported types and data conversion.TempDataandTempDataServicework correctly and as intended.Public API
ITempData
ITempDatais an interface for the customIDictionary. This is our base for theTempDataimplementation.TempData
This is a concrete implementation for the TempData. The crucial part here are methods
SaveandLoadthat are responsible for saving TempData to Cookie and loading data from Cookie respectively.Tests
Unit tests in
src/Components/Components/test/TempDataTest.cs:Getreturns value and removes from retained keysPeekreturns value without removing from retained keysKeep()retains all keys for another requestKeep(string)retains specific key for another requestContainsKeyreturns correct resultsRemoveremoves key and returns successSavereturns only retained keysLoadpopulates data from dictionaryClearremoves all dataUnit tests in
src/Components/Endpoints/test/TempDataServiceTest.cs:Loadreturns emptyTempDatawhen no cookie existsSavedeletes cookie when no data to saveSavesets cookie when data existsstring,int,bool,Guid,DateTimevaluesstringarrays andintarraysDictionary<string, string>Savethrows for unsupported typesLoadreturns empty TempData for invalid/corrupted cookiesE2E tests in
src/Components/test/E2ETest/Tests/TempDataTest.cs:Peekpreserves values for subsequent requestsKeep()retains all valuesKeep(key)retains specific valueRemove()deletes valuesContainsKeyworks correctlyAPI usage
Basic form with flash messages:
Reading without consuming (Peek):
Keeping values for another request:
Fixes #49683