TempData support for Blazor#64749
Conversation
…etcore into 49683-tempdata
73c510a to
6002ab1
Compare
javiercn
left a comment
There was a problem hiding this comment.
Looks good so far!
I haven't looked at the tests yet, but wanted to give the feedback so far.
src/Components/Endpoints/src/TempData/CookieTempDataProvider.cs
Outdated
Show resolved
Hide resolved
src/Components/Endpoints/src/TempData/CookieTempDataProvider.cs
Outdated
Show resolved
Hide resolved
src/Components/Endpoints/src/TempData/CookieTempDataProvider.cs
Outdated
Show resolved
Hide resolved
src/Components/Endpoints/src/TempData/CookieTempDataProvider.cs
Outdated
Show resolved
Hide resolved
src/Components/Endpoints/src/TempData/CookieTempDataProvider.cs
Outdated
Show resolved
Hide resolved
src/Components/Endpoints/src/TempData/TempDataProviderServiceCollectionExtensions.cs
Outdated
Show resolved
Hide resolved
ilonatommy
left a comment
There was a problem hiding this comment.
Reviewed framework changes.
src/Components/Endpoints/src/TempData/CookieTempDataProvider.cs
Outdated
Show resolved
Hide resolved
| HttpOnly = true, | ||
| SameSite = SameSiteMode.Lax, | ||
| IsEssential = false, | ||
| SecurePolicy = CookieSecurePolicy.SameAsRequest, |
There was a problem hiding this comment.
MVC has
Why did we decide to follow a different pattern?
There was a problem hiding this comment.
In XML doc comment it is mentioned that it defaults to the SameAsRequest. It was that way originally in MVC until #8043 changed it to None. I am pretty sure that currently this edge case, due to which it was changed, is extremely rare.
src/Components/Endpoints/src/TempData/JsonTempDataSerializer.cs
Outdated
Show resolved
Hide resolved
src/Components/Endpoints/src/TempData/JsonTempDataSerializer.cs
Outdated
Show resolved
Hide resolved
src/Components/Endpoints/src/Microsoft.AspNetCore.Components.Endpoints.csproj
Show resolved
Hide resolved
...stassets/Components.TestServer/RazorComponents/Pages/TempData/TempDataNotReadComponent.razor
Outdated
Show resolved
Hide resolved
c29267b to
eb5a42e
Compare
javiercn
left a comment
There was a problem hiding this comment.
@dariatiurina looks great.
One thing is that I don't see support for [SupplyParameterFromTempData] in this change. Is this expected in a follow up PR?
src/Components/Endpoints/src/DependencyInjection/RazorComponentsServiceOptions.cs
Outdated
Show resolved
Hide resolved
| JsonValueKind.Null => null, | ||
| JsonValueKind.True => true, | ||
| JsonValueKind.False => false, | ||
| JsonValueKind.Number => element.GetInt32(), |
There was a problem hiding this comment.
Is this what MVC does? This is incorrect, Number can be a floating-point value, so this can fail.
There was a problem hiding this comment.
Never mind this comment. I see MVC does exactly this.
|
|
||
| private static object? DeserializeString(JsonElement element) | ||
| { | ||
| var type = GetStringType(element); |
There was a problem hiding this comment.
This is also questionable (doing this type of "probbing/testing")
There was a problem hiding this comment.
Same. I just saw MVC does the same thing.
We should reconsider this a bit. We have to give it some thought to see if we can do better. One option would be to store the type information internally for round tripping.
TempData support for Blazor
Summary
This PR adds native TempData support for Blazor Server-Side Rendering (SSR), similar to the existing TempData in MVC. TempData provides a mechanism for storing data that persists between HTTP requests, making it ideal for scenarios like:
Changes
Core Components
ITempDataIDictionary<string, object?>TempDataITempDataProviderCookieTempDataProviderSessionStorageTempDataProviderJsonTempDataSerializerIntegration
TempData is automatically registered when calling
AddRazorComponents()and is provided as a cascading value. The default cookie-based provider is used unless explicitly configured otherwise.Public API
ITempData Interface
ITempDataProvider Interface
Service Collection Extensions
CookieTempDataProviderOptions
Tests
TempData behavior (
src/Components/Endpoints/test/TempData/TempDataTest.cs):Getreturns value and removes from retained keysPeekreturns value without removing from retained keysKeep()retains all keysKeep(string)retains specific keyContainsKeyreturns correct resultsRemoveremoves key and returns successSavereturns only retained keysLoadpopulates data from dictionaryClearremoves all dataWasLoadedtracks lazy loading stateJSON Serialization (
src/Components/Endpoints/test/TempData/JsonTempDataSerializerTest.cs):Cookie Provider (
src/Components/Endpoints/test/TempData/CookieTempDataProviderTest.cs):Service Registration (
src/Components/Endpoints/test/TempData/TempDataProviderServiceExtensionsTest.cs):E2E Tests
Integration tests (
src/Components/test/E2ETest/Tests/TempDataTest.cs):Peekpreserves values for subsequent requestsKeep()retains all valuesKeep(key)retains specific valueRemove()deletes valuesAPI usage
Basic form with flash messages:
Reading without consuming (Peek):
Keeping values for another request:
Design Decisions
Cascading Parameter: TempData is provided as a cascading value rather than requiring explicit injection, making it easily accessible throughout the component tree.
Lazy Loading: TempData is only loaded from storage when first accessed, avoiding unnecessary cookie parsing for requests that don't use TempData.
Cookie as Default: Cookies are the default storage mechanism (matching MVC behavior) because they don't require additional middleware configuration.
Data Protection: Cookie values are encrypted using ASP.NET Core Data Protection to prevent tampering and information disclosure.
Response.OnStarting: TempData is saved using
Response.OnStartingcallback to ensure it's persisted even if the component doesn't explicitly save it.Fixes #49683