Skip to content

Commit 4617328

Browse files
committed
Prevent form handling logic to run during exception handling
1 parent 4f0f3e3 commit 4617328

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/Components/Endpoints/src/RazorComponentEndpointInvoker.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Text.Encodings.Web;
88
using Microsoft.AspNetCore.Antiforgery;
99
using Microsoft.AspNetCore.Components.Endpoints.Rendering;
10+
using Microsoft.AspNetCore.Diagnostics;
1011
using Microsoft.AspNetCore.Http;
1112
using Microsoft.AspNetCore.WebUtilities;
1213
using Microsoft.Extensions.DependencyInjection;
@@ -128,7 +129,12 @@ await EndpointHtmlRenderer.InitializeStandardComponentServicesAsync(
128129

129130
private async Task<RequestValidationState> ValidateRequestAsync(HttpContext context, IAntiforgery? antiforgery)
130131
{
131-
var isPost = HttpMethods.IsPost(context.Request.Method);
132+
var isPost = HttpMethods.IsPost(context.Request.Method) &&
133+
// Disable POST functionality during exception handling.
134+
// The exception handler middleware will not update the request method, and we don't
135+
// want to run the form handling logic against the error page.
136+
context.Features.Get<IExceptionHandlerFeature>() == null;
137+
132138
if (isPost)
133139
{
134140
var valid = false;

src/Components/test/E2ETest/ServerRenderingTests/FormHandlingTests/FormWithParentBindingContextTest.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,15 +1119,13 @@ private void AssertHasInternalServerError(bool suppressedEnhancedNavigation, boo
11191119
{
11201120
Browser.True(() => Browser.FindElement(By.TagName("html")).Text.Contains("There was an unhandled exception on the current request"));
11211121
}
1122-
else if (suppressedEnhancedNavigation)
1123-
{
1124-
// Chrome's built-in error UI for a 500 response when there's no response content
1125-
Browser.Exists(By.Id("main-frame-error"));
1126-
}
11271122
else
11281123
{
1129-
// The UI generated by enhanced nav when there's no response content
1130-
Browser.Contains("Error: 500", () => Browser.Exists(By.TagName("html")).Text);
1124+
// Displays the error page from the exception handler
1125+
Assert.Collection(
1126+
Browser.FindElements(By.CssSelector(".text-danger")),
1127+
item => Assert.Equal("Error.", item.Text),
1128+
item => Assert.Equal("An error occurred while processing your request.", item.Text));
11311129
}
11321130
}
11331131

0 commit comments

Comments
 (0)