Skip to content

Commit ea737c7

Browse files
csharpfritzCopilot
andcommitted
fix: use OnAfterRender for interactive detection in WebFormsForm
HttpContextAccessor.HttpContext is never null in Blazor Server (even during SignalR circuits, it returns the connection's HTTP context). Replace with OnAfterRender-based detection which only fires on interactive circuits, never during prerendering or static SSR. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 18eef22 commit ea737c7

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

src/BlazorWebFormsComponents/WebFormsForm.razor

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
@namespace BlazorWebFormsComponents
22
@inherits ComponentBase
33
@using Microsoft.AspNetCore.Components
4-
@using Microsoft.AspNetCore.Http
54
@using Microsoft.Extensions.Primitives
65
@using Microsoft.JSInterop
76
@implements IAsyncDisposable
@@ -32,7 +31,6 @@ else
3231
private bool _isInteractive;
3332
private IJSObjectReference? _jsModule;
3433

35-
[Inject] private IHttpContextAccessor HttpContextAccessor { get; set; } = null!;
3634
[Inject] private IJSRuntime JSRuntime { get; set; } = null!;
3735

3836
/// <summary>
@@ -70,7 +68,17 @@ else
7068

7169
protected override void OnInitialized()
7270
{
73-
_isInteractive = HttpContextAccessor.HttpContext == null;
71+
// _isInteractive starts false; set in OnAfterRender which only runs
72+
// on interactive circuits (never during prerendering or static SSR).
73+
}
74+
75+
protected override void OnAfterRender(bool firstRender)
76+
{
77+
if (firstRender && !_isInteractive)
78+
{
79+
_isInteractive = true;
80+
StateHasChanged();
81+
}
7482
}
7583

7684
private async Task HandleSubmit()

0 commit comments

Comments
 (0)