SupplyParameterFromSession support for Blazor#65184
Open
dariatiurina wants to merge 14 commits intodotnet:mainfrom
Open
SupplyParameterFromSession support for Blazor#65184dariatiurina wants to merge 14 commits intodotnet:mainfrom
dariatiurina wants to merge 14 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. |
This was referenced Feb 9, 2026
f251239 to
ed0b04d
Compare
javiercn
reviewed
Apr 17, 2026
javiercn
reviewed
Apr 17, 2026
javiercn
reviewed
Apr 17, 2026
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
Adds a new
[SupplyParameterFromSession]attribute that allows Blazor SSR components to declaratively read and write HTTP session data, following the same pattern as[SupplyParameterFromQuery]and[SupplyParameterFromForm].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.
Changes
SupplyParameterFromSessionAttribute(Components.Web) — A new public attribute inheriting fromCascadingParameterAttributeBasewith an optionalNameproperty to specify the session key (defaults to the property name).SingleDeliveryisfalse, so values are re-supplied on each render.SessionCascadingValueSupplier(Components.Endpoints) — Internal scoped service that:ISessionviaISessionFeatureand deserializes them usingSystem.Text.Json(withJsonSerializerDefaults.Web)Response.OnStartingcallback to persist all tracked property values back to the session before the response is sentToLowerInvariant())PropertyGetterinstances per component type/property pair using aConcurrentDictionaryInvalidOperationExceptionif multiple components register the same keySessionSubscription(inner class ofSessionCascadingValueSupplier) — On the firstGetCurrentValue()call, reads from the session; on subsequent calls, returns the current component property value (enabling two-way binding). Removes the value callback onDispose.AddRazorComponents()now registersSessionCascadingValueSupplieras a scoped service and wires it as a cascading value supplier forSupplyParameterFromSessionAttribute.EndpointHtmlRenderer— CallsSessionCascadingValueSupplier.SetRequestContext(httpContext)during standard service initialization.PublicAPI.Unshipped.txt:SupplyParameterFromSessionAttributeclass, its constructor, andNameproperty.Usage
Developers must configure session middleware in their application:
Then use the attribute on component properties:
Values are read from the session on first render and automatically persisted back when the response starts.
Testing
SessionCascadingValueSupplierTest) — 10 tests covering: callback registration, duplicate key rejection, value persistence to session, null value removal, multiple keys, exception resilience (both callback and serialization errors), key lowercasing, no-op when session is unavailable, callback deletion, andOnStartingintegration.SessionSubscriptionTest) — 8 tests covering: null whenHttpContextnot set, null for missing keys, value retrieval, key lowercasing, enum deserialization, null on deserialization failure, component-value passthrough on subsequent calls, and fullCreateSubscriptionround-trip.SupplyParameterFromSessionAttributeTest) — 5 Selenium tests covering: reading from session, null for missing keys, redirect persistence, complex type serialization, and cross-navigation persistence.RazorComponentEndpointsNoInteractivityStartupto separate session middleware configuration (--UseSession) from TempData session storage (--UseSessionStorageTempDataProvider), addedMicrosoft.AspNetCore.Sessionreference to test server project, and added two test Razor components.Fixes #64422