[API Proposal]: Add SkipStatusCodePages property to ApiBehaviorOptions
See original GitHub issueBackground and Motivation
When mixing MVC Controllers with Api Controllers in an application, there should be a global setting in ApiBehaviorOptions to turn off the IStatusCodePagesFeature for Api Controllers to avoid overriding the raw api response with an MVC view/html based status code page implementation.
Proposed API
namespace Microsoft.AspNetCore.Mvc;
public class ApiBehaviorOptions : IEnumerable<ICompatibilitySwitch>
{
+    public bool SkipStatusCodePages { get; set; }
}
Usage Examples
// Program.cs Minimal API
var builder = WebApplication.CreateBuilder(args);
// Register services
app.AddMvc().ConfigureApiBehaviorOptions(options =>
{
    options.SkipStatusCodePages = true;
});
var app = builder.Build();
// Register middlewares
app.MapControllers();
await app.RunAsync();
Alternative Designs
public static class SkipStatusCodePagesMetadataExtensions
{
    public static IEndpointConventionBuilder SkipStatusCodePagesForApiControllers(this IEndpointConventionBuilder builder)
    {
        builder.Add(endpointBuilder =>
        {
            var apiControllerAttribute = endpointBuilder.Metadata.FirstOrDefault(m => m.GetType() == typeof(ApiControllerAttribute)) as ApiControllerAttribute;
            if (apiControllerAttribute == null)
            {
                return;
            }
            endpointBuilder.Metadata.Add(new SkipStatusCodePagesMetadata());
            endpointBuilder.FilterFactories.Add((context, next) =>
            {
                return async context =>
                {
                    var statusCodeFeature = context.HttpContext.Features.Get<IStatusCodePagesFeature>();
                    if (statusCodeFeature != null)
                    {
                        // Turn off the StatusCodePages feature.
                        statusCodeFeature.Enabled = false;
                    }
                    return await next(context);
                };
            });
        });
        return builder;
    }
}
 // Marker metadata class
file class SkipStatusCodePagesMetadata : ISkipStatusCodePagesMetadata
{
}
// Program.cs Minimal API
var builder = WebApplication.CreateBuilder(args);
// Register services
app.AddMvc();
var app = builder.Build();
// Register middlewares
app.MapControllers().SkipStatusCodePagesForApiControllers();
await app.RunAsync();
Risks
There are no risks because the default value for ApiBehaviorOptions.SkipStatusCodePages will be false which is the current behavior.
Issue Analytics
- State:
 - Created 10 months ago
 - Comments:5 (4 by maintainers)
 
Top Results From Across the Web
ApiBehaviorOptions Class (Microsoft.AspNetCore.Mvc)
Gets or sets a value that determines if an multipart/form-data consumes action constraint is added to parameters that are bound from form data....
Read more >How to Use ModelState Validation in ASP.NET Core Web API
When we talk about ModelState , we mean ModelState property of the ControllerBase abstract class in the Microsoft.AspNetCore.Mvc namespace.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@halter73 Thanks for pointing that out! With the correct ordering the need to add a filter is not required.
But this will also skip the StatusCodePagesMiddleware for MVC Controllers which is not desired.
Here is a minimal repo about this issue.
API Review Notes:
UseStatusCodePagessimply doesn’t work for API controllers without an opt-in?ApiBehaviorOptions, but it does align with the attribute name..MapControllers().WithMetadata(new SkipStatusCodePagesAttribute())works to skip status code pages for all controllers, this will be specific to API Controllers.API Approved as proposed!