Skip to content

Commit 9dd048a

Browse files
authored
Support SkipStatusCodePages on endpoints and authorized routes (#38509)
* Support SkipStatusCodePages on endpoints and authorized routes * Address feedback from API review * Fix sort of usings
1 parent 297aeef commit 9dd048a

File tree

6 files changed

+59
-1
lines changed

6 files changed

+59
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.AspNetCore.Http.Metadata;
5+
6+
/// <summary>
7+
/// Defines a contract used to specify metadata for skipping the StatusCodePage
8+
/// middleware in <see cref="Endpoint.Metadata"/>.
9+
/// </summary>
10+
public interface ISkipStatusCodePagesMetadata
11+
{
12+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
#nullable enable
22
*REMOVED*abstract Microsoft.AspNetCore.Http.HttpResponse.ContentType.get -> string!
33
abstract Microsoft.AspNetCore.Http.HttpResponse.ContentType.get -> string?
4+
Microsoft.AspNetCore.Http.Metadata.ISkipStatusCodePagesMetadata

src/Middleware/Diagnostics/src/StatusCodePage/StatusCodePagesMiddleware.cs

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Threading.Tasks;
66
using Microsoft.AspNetCore.Builder;
77
using Microsoft.AspNetCore.Http;
8+
using Microsoft.AspNetCore.Http.Metadata;
89
using Microsoft.Extensions.Options;
910

1011
namespace Microsoft.AspNetCore.Diagnostics;
@@ -41,6 +42,13 @@ public async Task Invoke(HttpContext context)
4142
{
4243
var statusCodeFeature = new StatusCodePagesFeature();
4344
context.Features.Set<IStatusCodePagesFeature>(statusCodeFeature);
45+
var endpoint = context.GetEndpoint();
46+
var skipStatusCodePageMetadata = endpoint?.Metadata.GetMetadata<ISkipStatusCodePagesMetadata>();
47+
48+
if (skipStatusCodePageMetadata is not null)
49+
{
50+
statusCodeFeature.Enabled = false;
51+
}
4452

4553
await _next(context);
4654

src/Middleware/Diagnostics/test/UnitTests/Microsoft.AspNetCore.Diagnostics.Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<Reference Include="Microsoft.Extensions.DiagnosticAdapter" />
2121
<Reference Include="Microsoft.Extensions.FileProviders.Embedded" />
2222
<Reference Include="Microsoft.AspNetCore" />
23+
<Reference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" />
2324
</ItemGroup>
2425

2526
</Project>

src/Middleware/Diagnostics/test/UnitTests/StatusCodeMiddlewareTest.cs

+35
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.AspNetCore.Builder;
99
using Microsoft.AspNetCore.Hosting;
1010
using Microsoft.AspNetCore.Http;
11+
using Microsoft.AspNetCore.Mvc;
1112
using Microsoft.AspNetCore.TestHost;
1213
using Microsoft.Extensions.DependencyInjection;
1314
using Microsoft.Extensions.Hosting;
@@ -283,4 +284,38 @@ public async Task Reexecute_WorksAfterUseRoutingWithGlobalRouteBuilder()
283284
var content = await response.Content.ReadAsStringAsync();
284285
Assert.Equal("errorPage", content);
285286
}
287+
288+
[Fact]
289+
public async Task SkipStatusCodePages_SupportsEndpoints()
290+
{
291+
var builder = WebApplication.CreateBuilder();
292+
builder.WebHost.UseTestServer();
293+
await using var app = builder.Build();
294+
295+
app.UseRouting();
296+
297+
app.UseStatusCodePages();
298+
299+
app.UseEndpoints(endpoints =>
300+
{
301+
endpoints.MapGet("/", [SkipStatusCodePages] (c) =>
302+
{
303+
c.Response.StatusCode = 404;
304+
return Task.CompletedTask;
305+
});
306+
});
307+
308+
app.Run((context) =>
309+
{
310+
throw new InvalidOperationException("Invalid input provided.");
311+
});
312+
313+
await app.StartAsync();
314+
315+
using var server = app.GetTestServer();
316+
var client = server.CreateClient();
317+
var response = await client.GetAsync("/");
318+
var content = await response.Content.ReadAsStringAsync();
319+
Assert.Empty(content);
320+
}
286321
}

src/Mvc/Mvc.ViewFeatures/src/SkipStatusCodePagesAttribute.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using Microsoft.AspNetCore.Diagnostics;
6+
using Microsoft.AspNetCore.Http.Metadata;
67
using Microsoft.AspNetCore.Mvc.Filters;
78

89
namespace Microsoft.AspNetCore.Mvc;
@@ -11,7 +12,7 @@ namespace Microsoft.AspNetCore.Mvc;
1112
/// A filter that prevents execution of the StatusCodePages middleware.
1213
/// </summary>
1314
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
14-
public class SkipStatusCodePagesAttribute : Attribute, IResourceFilter
15+
public class SkipStatusCodePagesAttribute : Attribute, IResourceFilter, ISkipStatusCodePagesMetadata
1516
{
1617
/// <inheritdoc />
1718
public void OnResourceExecuted(ResourceExecutedContext context)

0 commit comments

Comments
 (0)