Skip to content

Support SkipStatusCodePages when not using WebApplication builder and UseStatusCodePagesWithReExecute #46109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public async Task Invoke(HttpContext context)
var statusCodeFeature = new StatusCodePagesFeature();
context.Features.Set<IStatusCodePagesFeature>(statusCodeFeature);
var endpoint = context.GetEndpoint();
var skipStatusCodePageMetadata = endpoint?.Metadata.GetMetadata<ISkipStatusCodePagesMetadata>();
var shouldCheckEndpointAgain = endpoint is null;

if (skipStatusCodePageMetadata is not null)
if (HasSkipStatusCodePagesMetadata(endpoint))
{
statusCodeFeature.Enabled = false;
}
Expand All @@ -57,6 +57,12 @@ public async Task Invoke(HttpContext context)
return;
}

if (shouldCheckEndpointAgain && HasSkipStatusCodePagesMetadata(context.GetEndpoint()))
{
// If the endpoint was null check the endpoint again since it could have been set by another middleware.
return;
}

// Do nothing if a response body has already been provided.
if (context.Response.HasStarted
|| context.Response.StatusCode < 400
Expand All @@ -70,4 +76,11 @@ public async Task Invoke(HttpContext context)
var statusCodeContext = new StatusCodeContext(context, _options, _next);
await _options.HandleAsync(statusCodeContext);
}

private static bool HasSkipStatusCodePagesMetadata(Endpoint? endpoint)
{
var skipStatusCodePageMetadata = endpoint?.Metadata.GetMetadata<ISkipStatusCodePagesMetadata>();

return skipStatusCodePageMetadata is not null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -313,4 +313,80 @@ public async Task SkipStatusCodePages_SupportsEndpoints()
var content = await response.Content.ReadAsStringAsync();
Assert.Empty(content);
}

[Fact]
public async Task SkipStatusCodePages_SupportsSkipIfUsedBeforeRouting()
{
using var host = new HostBuilder()
.ConfigureWebHost(builder =>
{
builder.UseTestServer()
.ConfigureServices(services => services.AddRouting())
.Configure(app =>
{
app.UseStatusCodePagesWithReExecute("/status");
app.UseRouting();

app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/skip", [SkipStatusCodePages](c) =>
{
c.Response.StatusCode = 400;
return Task.CompletedTask;
});

endpoints.MapGet("/status", (HttpResponse response) => $"Status: {response.StatusCode}");
});

app.Run(_ => throw new InvalidOperationException("Invalid input provided."));
});
}).Build();

await host.StartAsync();

using var server = host.GetTestServer();
var client = server.CreateClient();
var response = await client.GetAsync("/skip");
var content = await response.Content.ReadAsStringAsync();

Assert.Empty(content);
}

[Fact]
public async Task SkipStatusCodePages_WorksIfUsedBeforeRouting()
{
using var host = new HostBuilder()
.ConfigureWebHost(builder =>
{
builder.UseTestServer()
.ConfigureServices(services => services.AddRouting())
.Configure(app =>
{
app.UseStatusCodePagesWithReExecute("/status");
app.UseRouting();

app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", (c) =>
{
c.Response.StatusCode = 400;
return Task.CompletedTask;
});

endpoints.MapGet("/status", (HttpResponse response) => $"Status: {response.StatusCode}");
});

app.Run(_ => throw new InvalidOperationException("Invalid input provided."));
});
}).Build();

await host.StartAsync();

using var server = host.GetTestServer();
var client = server.CreateClient();
var response = await client.GetAsync("/");
var content = await response.Content.ReadAsStringAsync();

Assert.Equal("Status: 400", content);
}
}