SupplyParameterFromSession support for Blazor #65184
Draft
+799
−0
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.
SupplyParameterFromSession support for Blazor
Summary
Enable Blazor SSR developers to easily read and write session data using a declarative
[SupplyParameterFromSession]attribute, providing a familiar pattern consistent with existing[SupplyParameterFromQuery]and[SupplyParameterFromForm]attributes.Motivation
Currently, Blazor SSR lacks a simple, declarative way to access session data. Developers who want to persist user-specific data across HTTP requests (like shopping cart contents or multi-step form state) must:
IHttpContextAccessorand manually interact withISessionThis creates inconsistent patterns across applications and increases the likelihood of errors. MVC Razor Pages developers have easier access to session data, and Blazor SSR should offer a similar experience.
Goals
[SupplyParameterFromSession]NamepropertyAddRazorComponents()setupNon-Goals
Scenarios
Scenario 1: Multi-step form wizard
As a developer, I want to preserve form data across multiple pages so that users don't lose their input when navigating between steps.
Scenario 2: User preferences
As a developer, I want to store user preferences (like theme or language) in the session so that they persist across page navigations without requiring database storage.
Scenario 3: Shopping cart (before checkout)
As a developer, I want to maintain a shopping cart across pages so users can continue browsing and adding items.
Detailed Design
Core Components
1. SupplyParameterFromSessionAttribute
A new attribute that inherits from
CascadingParameterAttributeBase:2. ISessionValueMapper Interface
3. SessionValueMapper Implementation
The
SessionValueMapperclass handles:System.Text.JsonLifecycle Flow
We do not use
Unsubcribedue to complicated situations with the disposal of the components in SSR.Usage Examples
Basic usage - reading and writing:
Custom session key name:
Complex types:
Multi-step form:
Supported Types
string,int,bool,double,decimal, etc.Guid,DateTime,DateTimeOffsetint?,DateTime?, etc.List<T>,Dictionary<string, T>, arraysRegistration
The feature is automatically enabled when using
AddRazorComponents():Developers must still configure session middleware in their application:
Risks and Unknowns
Risk 1: Session size limits
Developers may store excessive data in session, impacting performance.
Risk 2: Serialization failures
Complex types may fail to serialize/deserialize.
Risk 3: Session not configured
Developers may forget to configure session middleware.
Unknown: Performance impact
Need to measure the overhead of JSON serialization/deserialization per request.
Drawbacks
Considered Alternatives
Alternative 1: TempData approach
Reuse MVC's TempData infrastructure.
Why rejected: TempData has single-read semantics (values are removed after reading), which doesn't fit session persistence use cases.
Alternative 2: ProtectedSessionStorage
Use the existing
ProtectedSessionStoragefromMicrosoft.AspNetCore.Components.Server.ProtectedBrowserStorage.Why rejected: Requires JavaScript interop, which is incompatible with Blazor SSR's static rendering model.
Alternative 3: Direct ISession injection
Developers inject
IHttpContextAccessorand useISessiondirectly.Why rejected: Works but is verbose and inconsistent with other
SupplyParameterFrom*patterns. This proposal provides a better developer experience while still usingISessionunder the hood.Examples
Complete multi-page form example
Step1.razor:
Step2.razor:
Open Questions
Fixes #64422