-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Blazor] Allow
null
parameter values to be supplied to interactive …
…components via enhanced page update (#53317) (#53342) # Allow `null` parameter values to be supplied to interactive components via enhanced page update Backport of #53317 Fixes an issue where a `NullRefrenceException` would be thrown if an enhanced page update supplied a `null` parameter to an interactive root component. ## Description In .NET 8, SSR'd components can supply updated parameters to existing interactive root components. If one of those updated parameters is `null`, an exception currently gets thrown from within the framework. This causes the circuit to crash when using Server interactivity, and it would causes an error to be logged in the browser console when using WebAssembly interactivity. This PR fixes the problem by treating `null` as a valid value for a serialized parameter that gets supplied to an interactive root component. Fixes #52434 ## Customer Impact Without this fix, customers may encounter the unfriendly exception and have a hard time figuring out the underlying cause. We have not yet seen customer reports of the issue, but it's possible that customers have this bug in their apps without knowing it, especially since it only occurs when supplying updated parameters to existing components (not when supplying the initial set of parameters). One workaround would be to use a different value than `null` to specify an empty parameter value, but this may not be possible in cases where the parameter gets supplied by the framework (e.g., via route value), or if the interactive root component's implementation is not under the developer's control. ## Regression? - [ ] Yes - [X] No Only applicable to new scenarios in .NET 8. ## Risk - [ ] High - [ ] Medium - [X] Low The fix is straightforward and well-tested. ## Verification - [x] Manual (required) - [x] Automated ## Packaging changes reviewed? - [ ] Yes - [ ] No - [X] N/A
- Loading branch information
1 parent
658ddfb
commit e91e94d
Showing
6 changed files
with
167 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
24 changes: 24 additions & 0 deletions
24
...estServer/RazorComponents/Pages/EnhancedNav/PageRenderingComponentWithNullParameter.razor
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
@page "/nav/null-parameter/{mode}" | ||
@using TestContentPackage | ||
|
||
@* https://github.com/dotnet/aspnetcore/issues/52434 *@ | ||
|
||
<h1>Page rendering component with null parameter</h1> | ||
|
||
@if (Mode == "server") | ||
{ | ||
<ComponentAcceptingNullParameter @rendermode="RenderMode.InteractiveServer" Value="@null" /> | ||
} | ||
else if (Mode == "wasm") | ||
{ | ||
<ComponentAcceptingNullParameter @rendermode="RenderMode.InteractiveWebAssembly" Value="@null" /> | ||
} | ||
else | ||
{ | ||
<p>Expected a render mode of 'server' or 'wasm', but got '@Mode'.</p> | ||
} | ||
|
||
@code { | ||
[Parameter] | ||
public string? Mode { get; set; } | ||
} |
This file contains 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
52 changes: 52 additions & 0 deletions
52
src/Components/test/testassets/TestContentPackage/ComponentAcceptingNullParameter.razor
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
@implements IDisposable | ||
@inject NavigationManager NavigationManager | ||
@using Microsoft.AspNetCore.Components.Routing | ||
|
||
<p>Value: @(Value ?? "(null)")</p> | ||
|
||
@if (_interactive) | ||
{ | ||
<button id="button-increment" @onclick="Increment">Count: <span id="current-count">@_count</span></button> | ||
<button id="button-refresh" @onclick="Refresh">Refresh</button> | ||
<p>Location changed count: <span id="location-changed-count">@_locationChangedCount</span></p> | ||
} | ||
|
||
@code { | ||
private bool _interactive; | ||
private int _count; | ||
private int _locationChangedCount; | ||
|
||
[Parameter] | ||
public string Value { get; set; } | ||
|
||
protected override void OnAfterRender(bool firstRender) | ||
{ | ||
if (firstRender) | ||
{ | ||
NavigationManager.LocationChanged += OnLocationChanged; | ||
_interactive = true; | ||
StateHasChanged(); | ||
} | ||
} | ||
|
||
private void OnLocationChanged(object sender, LocationChangedEventArgs e) | ||
{ | ||
_locationChangedCount++; | ||
StateHasChanged(); | ||
} | ||
|
||
private void Increment() | ||
{ | ||
_count++; | ||
} | ||
|
||
private void Refresh() | ||
{ | ||
NavigationManager.Refresh(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
NavigationManager.LocationChanged -= OnLocationChanged; | ||
} | ||
} |