Skip to content

Commit 298c178

Browse files
committed
Additional cleanups
1 parent 7d72205 commit 298c178

File tree

13 files changed

+52
-30
lines changed

13 files changed

+52
-30
lines changed

src/Components/Components/src/Binding/CascadingModelBinder.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ public sealed class CascadingModelBinder : IComponent, IDisposable
2222
[Parameter] public string Name { get; set; } = "";
2323

2424
/// <summary>
25-
/// The binding context name.
25+
/// If true, indicates that <see cref="ModelBindingContext.BindingContextId"/> will not change.
26+
/// This is a performance optimization that allows the framework to skip setting up
27+
/// change notifications. Set this flag only if you will not change
28+
/// <see cref="Name"/> of this context or its parents' context during the component's lifetime.
2629
/// </summary>
2730
[Parameter] public bool IsFixed { get; set; }
2831

src/Components/Components/src/RenderTree/RenderTreeDiffBuilder.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,12 @@ public static RenderTreeDiff ComputeDiff(
4040
// Once a component has defined a named event handler with a concrete name, no other component instance can
4141
// define a named event handler with that name.
4242
//
43-
// This is only enforced when we are trying to dispatch an event to a named event handler, since in any other
44-
// case we don't actually track the named event handlers.
45-
//
46-
// We don't enforce the global uniqueness of the event handler name inside the base renderer either. That's
47-
// handled by the EndpointRenderer that takes care of ensuring that only one component defined the expected
48-
// named event handler before dispatching the named event.
49-
//
5043
// At this stage, we only ensure that the named event handler is unique per component instance, as that,
5144
// combined with the check that the EndpointRenderer does, is enough to ensure the uniqueness and the stability
5245
// of the named event handler over time **globally**.
5346
//
54-
// Note that we only enforce this condition at the time we are going to dispatch a named event to a specific
55-
// named event handler. In any other case, even though is an error, we don't care, as:
47+
// Tracking and uniqueness are enforced when we are trying to dispatch an event to a named event handler, since in
48+
// any other case we don't actually track the named event handlers. We do this because:
5649
// 1) We don't want to break the user's app if we don't have to.
5750
// 2) We don't have to pay the cost of continously tracking all events all the time to throw.
5851
// That's why raising the error is delayed until we are forced to make a decission.

src/Components/Components/src/RenderTree/Renderer.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -419,13 +419,10 @@ public virtual Task DispatchEventAsync(ulong eventHandlerId, EventFieldInfo? fie
419419
if (quiesce)
420420
{
421421
_pendingTasks ??= new();
422-
task = callback.InvokeAsync(eventArgs);
423-
AddToPendingTasksWithErrorHandling(task, receiverComponentState);
424-
}
425-
else
426-
{
427-
task = callback.InvokeAsync(eventArgs);
428422
}
423+
424+
task = callback.InvokeAsync(eventArgs);
425+
AddToPendingTasksWithErrorHandling(task, receiverComponentState);
429426
}
430427
catch (Exception e)
431428
{

src/Components/Components/src/RouteView.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,6 @@ protected virtual void Render(RenderTreeBuilder builder)
7777

7878
private void RenderPageWithParameters(RenderTreeBuilder builder)
7979
{
80-
var pathStart = NavigationManager.BaseUri.Length - 1;
81-
var bindingId = NavigationManager.Uri.Substring(
82-
pathStart,
83-
NavigationManager.Uri.AsSpan().IndexOfAny("?#") switch
84-
{
85-
-1 => NavigationManager.Uri.Length - pathStart,
86-
var index => index - pathStart
87-
});
88-
8980
builder.OpenComponent<CascadingModelBinder>(0);
9081
builder.AddComponentParameter(1, nameof(CascadingModelBinder.ChildContent), (RenderFragment<ModelBindingContext>)RenderPageWithContext);
9182
builder.CloseComponent();

src/Components/Endpoints/src/RazorComponentEndpointInvoker.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ private async Task RenderComponentCore()
5959
hostParameters,
6060
waitForQuiescence: isPost);
6161

62+
if (isPost && !_renderer.HasCapturedEvent())
63+
{
64+
_context.Response.StatusCode = StatusCodes.Status404NotFound;
65+
}
66+
6267
var quiesceTask = isPost ? _renderer.DispatchCapturedEvent() : htmlContent.QuiescenceTask;
6368

6469
if (isPost)

src/Components/Endpoints/src/Rendering/EndpointHtmlRenderer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ protected override void TrackNamedEventId(ulong eventHandlerId, int componentId,
119119
}
120120
}
121121

122+
internal bool HasCapturedEvent() => _capturedNamedEvent != default;
123+
122124
internal Task DispatchCapturedEvent()
123125
{
124126
if (_capturedNamedEvent == default)

src/Components/test/E2ETest/ServerRenderingTests/FormHandlingTest.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ public void CanDispatchToNamedForm()
4949
DispatchToFormCore(dispatchToForm);
5050
}
5151

52+
[Fact]
53+
public void CanDispatchToNamedFormNoParentBindingContext()
54+
{
55+
var dispatchToForm = new DispatchToForm(this)
56+
{
57+
Url = "forms/named-form-no-form-context",
58+
FormCssSelector = "form[name=named-form-handler]",
59+
ExpectedActionValue = "forms/named-form-no-form-context?handler=named-form-handler",
60+
};
61+
DispatchToFormCore(dispatchToForm);
62+
}
63+
5264
[Fact]
5365
public void CanDispatchToNamedFormInNestedContext()
5466
{

src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/Forms/FormOutsideBindingContextNoOps.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
@using Microsoft.AspNetCore.Components.Forms
33
@layout NoFormContextLayout
44

5-
<h2>Default form</h2>
5+
<h2>Form outside a cascading binding context no-ops</h2>
66

77
<EditForm EditContext="_editContext" method="POST" OnValidSubmit="() => _submitted = true" >
88
<input id="send" type="submit" value="Send" />

src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/Forms/ModifyHttpContextForm.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
@using Microsoft.AspNetCore.Components.Forms
99
@using Microsoft.AspNetCore.Mvc;
1010

11-
<h2>Default form</h2>
11+
<h2>Event handler sets cookie during form POST</h2>
1212

1313
<EditForm EditContext="_editContext" method="POST" OnValidSubmit="HandleSubmit">
1414
<input id="send" type="submit" value="Send" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
@page "/forms/named-form-no-form-context"
2+
@layout NoFormContextLayout
3+
@using Microsoft.AspNetCore.Components.Forms
4+
5+
<h2>Named form</h2>
6+
7+
<EditForm FormHandlerName="named-form-handler" EditContext="_editContext" method="POST" OnValidSubmit="() => _submitted = true" >
8+
<input id="send" type="submit" value="Send" />
9+
</EditForm>
10+
11+
@if (_submitted)
12+
{
13+
<p id="pass">Form submitted!</p>
14+
}
15+
16+
@code{
17+
bool _submitted = false;
18+
EditContext _editContext = new EditContext(new object());
19+
}

src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/Forms/NonStreamingRenderingForm.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
@using Microsoft.AspNetCore.Components.Forms
66
@using Microsoft.AspNetCore.Mvc;
77

8-
<h2>Default form</h2>
8+
<h2>Non streaming async form</h2>
99

1010
<EditForm EditContext="_editContext" method="POST" OnValidSubmit="HandleSubmit">
1111
<input id="send" type="submit" value="Send" />

src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/Forms/StreamingRenderingForm.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
@using Microsoft.AspNetCore.Components.Forms
77
@using Microsoft.AspNetCore.Mvc;
88

9-
<h2>Default form</h2>
9+
<h2>Streaming rendering async form</h2>
1010

1111
<EditForm EditContext="_editContext" method="POST" OnValidSubmit="HandleSubmit">
1212
<input id="send" type="submit" value="Send" />

src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/Forms/SwitchingDispatchedComponentsDoesNotBind.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
@using Components.TestServer.RazorComponents
33
@using Microsoft.AspNetCore.Components.Authorization
44

5-
<h2>Form disappears before dispatching</h2>
5+
<h2>Form component instance changes before dispatching</h2>
66

77
@if (!_ready)
88
{

0 commit comments

Comments
 (0)