SupplyParameterFromSession support for Blazor#65184
Open
dariatiurina wants to merge 11 commits intodotnet:mainfrom
Open
SupplyParameterFromSession support for Blazor#65184dariatiurina wants to merge 11 commits intodotnet:mainfrom
dariatiurina wants to merge 11 commits intodotnet:mainfrom
Conversation
This was referenced Jan 22, 2026
Open
Contributor
There was a problem hiding this comment.
Pull request overview
Adds [SupplyParameterFromSession] support for Blazor SSR by introducing a session-backed cascading value supplier and request-scoped session value mapper, plus test assets and E2E/unit tests to validate behavior.
Changes:
- Introduces
SupplyParameterFromSessionAttribute, session value provider, andISessionValueMapper/SessionValueMapper. - Wires session value mapper into
EndpointHtmlRendererrequest initialization and registers services viaAddRazorComponents(). - Adds test server pages and E2E/unit tests for reading/writing session-backed parameters across navigations.
Reviewed changes
Copilot reviewed 18 out of 18 changed files in this pull request and generated 11 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/SupplyParameterFromSessionNavigationComponent.razor | Adds navigation test page used to validate session persistence across redirects. |
| src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/SupplyParameterFromSessionComponent.razor | Adds SSR test page exercising primitive, named-key, null, and complex session values. |
| src/Components/test/testassets/Components.TestServer/RazorComponentEndpointsNoInteractivityStartup.cs | Adds conditional session service/middleware setup for the tests. |
| src/Components/test/testassets/Components.TestServer/Components.TestServer.csproj | Adds Session assembly reference for the test server. |
| src/Components/test/E2ETest/Tests/SupplyParameterFromSessionAttributeTest.cs | Adds E2E coverage for session parameter read/write and navigation persistence. |
| src/Components/Endpoints/test/SessionValueMapperTest.cs | Adds unit tests for SessionValueMapper serialization, persistence, and error handling. |
| src/Components/Endpoints/src/Session/SupplyParameterFromSessionValueProvider.cs | Implements the cascading value supplier that binds component properties to session keys. |
| src/Components/Endpoints/src/Session/SupplyParameterFromSessionServiceCollectionExtensions.cs | Adds DI extension to register the session value supplier. |
| src/Components/Endpoints/src/Session/SessionValueMapper.cs | Implements session read/write with JSON serialization and response on-start persistence. |
| src/Components/Endpoints/src/Session/ISessionValueMapper.cs | Adds public contract for session value mapping and persistence callbacks. |
| src/Components/Endpoints/src/Rendering/EndpointHtmlRenderer.cs | Initializes the session mapper with the current request HttpContext. |
| src/Components/Endpoints/src/Rendering/EndpointHtmlRenderer.Prerendering.cs | Widens accessibility of overrides (likely to enable cross-assembly usage). |
| src/Components/Endpoints/src/Rendering/EndpointComponentState.cs | Widens accessibility of GetComponentKey override. |
| src/Components/Endpoints/src/PublicAPI.Unshipped.txt | Declares newly added public APIs for the Endpoints assembly. |
| src/Components/Endpoints/src/DependencyInjection/RazorComponentsServiceCollectionExtensions.cs | Registers ISessionValueMapper and session value provider in AddRazorComponents(). |
| src/Components/Components/src/SupplyParameterFromSessionAttribute.cs | Adds the public attribute for supplying values from session. |
| src/Components/Components/src/PublicAPI.Unshipped.txt | Declares newly added public APIs for the Components assembly. |
| src/Components/Components/src/Microsoft.AspNetCore.Components.csproj | Adds internals visibility to allow Endpoints to consume internal attribute members. |
src/Components/Components/src/SupplyParameterFromSessionValueProvider.cs
Show resolved
Hide resolved
src/Components/Endpoints/src/Session/SupplyParameterFromSessionValueProvider.cs
Outdated
Show resolved
Hide resolved
...assets/Components.TestServer/RazorComponents/Pages/SupplyParameterFromSessionComponent.razor
Outdated
Show resolved
Hide resolved
src/Components/Components/src/SupplyParameterFromSessionAttribute.cs
Outdated
Show resolved
Hide resolved
...ponents.TestServer/RazorComponents/Pages/SupplyParameterFromSessionNavigationComponent.razor
Outdated
Show resolved
Hide resolved
...ponents.TestServer/RazorComponents/Pages/SupplyParameterFromSessionNavigationComponent.razor
Show resolved
Hide resolved
This was referenced Feb 9, 2026
f251239 to
ed0b04d
Compare
Contributor
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
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.
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