Skip to content
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

feat: extending ApiBehaviorOptions with skip SkipStatusCodePages #57597

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions src/Mvc/Mvc.Core/src/ApiBehaviorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ public Func<ActionContext, IActionResult> InvalidModelStateResponseFactory
/// </summary>
public bool SuppressConsumesConstraintForFormFileParameters { get; set; }

/// <summary>
/// Gets or sets a value that determines whether the is suppressed for
/// API controllers. This feature prevents MVC's status code pages from overriding raw API responses with
/// HTML-based status code pages when mixing MVC Controllers with API Controllers in the same application.
/// </summary>
public bool SkipStatusCodePages { get; set; }

/// <summary>
/// Gets or sets a value that determines if controllers with <see cref="ApiControllerAttribute"/>
/// transform certain client errors.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public ApiBehaviorApplicationModelProvider(
ActionModelConventions.Add(new ConsumesConstraintForFormFileParameterConvention());
}

if (options.SkipStatusCodePages)
{
ActionModelConventions.Add(new SkipStatusCodePagesConvention());
}

var defaultErrorType = options.SuppressMapClientErrors ? typeof(void) : typeof(ProblemDetails);
var defaultErrorTypeAttribute = new ProducesErrorResponseTypeAttribute(defaultErrorType);
ActionModelConventions.Add(new ApiConventionApplicationModelConvention(defaultErrorTypeAttribute));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.AspNetCore.Mvc.ApplicationModels;

/// <summary>
/// An <see cref="IActionModelConvention"/> that adds a <see cref="SkipStatusCodePagesAttribute"/>
/// to controllers
/// </summary>
public class SkipStatusCodePagesConvention : IActionModelConvention
{
/// <inheritdoc />
public void Apply(ActionModel action)
{
ArgumentNullException.ThrowIfNull(action);

if (!ShouldApply(action))
{
return;
}

AddSkipStatusCodePagesAttribute(action);
}

/// <summary>
/// Determines if this instance of <see cref="IActionModelConvention"/> applies to a specified <paramref name="action"/>.
/// </summary>
/// <param name="action">The <see cref="ActionModel"/>.</param>
/// <returns>
/// <see langword="true"/> if the convention applies, otherwise <see langword="false"/>.
/// Derived types may override this method to selectively apply this convention.
/// </returns>
protected virtual bool ShouldApply(ActionModel action) => true;

// Internal for unit testing
internal static void AddSkipStatusCodePagesAttribute(ActionModel action)
{
action.Filters.Add(new SkipStatusCodePagesAttribute());

Check failure on line 38 in src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux ARM)

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs#L38

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs(38,32): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'SkipStatusCodePagesAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 38 in src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux Musl x64)

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs#L38

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs(38,32): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'SkipStatusCodePagesAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 38 in src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: macOS x64)

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs#L38

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs(38,32): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'SkipStatusCodePagesAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 38 in src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux Musl ARM64)

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs#L38

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs(38,32): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'SkipStatusCodePagesAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 38 in src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux Musl ARM)

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs#L38

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs(38,32): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'SkipStatusCodePagesAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 38 in src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux ARM64)

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs#L38

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs(38,32): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'SkipStatusCodePagesAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 38 in src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: macOS arm64)

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs#L38

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs(38,32): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'SkipStatusCodePagesAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 38 in src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux x64)

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs#L38

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs(38,32): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'SkipStatusCodePagesAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 38 in src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr (Tests: Ubuntu x64)

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs#L38

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs(38,32): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'SkipStatusCodePagesAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 38 in src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Test: Ubuntu x64)

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs#L38

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs(38,32): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'SkipStatusCodePagesAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 38 in src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr (Tests: macOS)

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs#L38

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs(38,32): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'SkipStatusCodePagesAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 38 in src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs#L38

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs(38,32): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'SkipStatusCodePagesAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 38 in src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs#L38

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs(38,32): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'SkipStatusCodePagesAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 38 in src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Test: macOS)

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs#L38

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs(38,32): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'SkipStatusCodePagesAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 38 in src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs#L38

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs(38,32): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'SkipStatusCodePagesAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 38 in src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs#L38

src/Mvc/Mvc.Core/src/ApplicationModels/SkipStatusCodePagesConvention.cs(38,32): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'SkipStatusCodePagesAttribute' could not be found (are you missing a using directive or an assembly reference?)
return;
}
}
6 changes: 6 additions & 0 deletions src/Mvc/Mvc.Core/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#nullable enable
*REMOVED*virtual Microsoft.AspNetCore.Mvc.ControllerBase.Problem(string? detail = null, string? instance = null, int? statusCode = null, string? title = null, string? type = null) -> Microsoft.AspNetCore.Mvc.ObjectResult!
*REMOVED*virtual Microsoft.AspNetCore.Mvc.ControllerBase.ValidationProblem(string? detail = null, string? instance = null, int? statusCode = null, string? title = null, string? type = null, Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary? modelStateDictionary = null) -> Microsoft.AspNetCore.Mvc.ActionResult!
Microsoft.AspNetCore.Mvc.ApiBehaviorOptions.SkipStatusCodePages.get -> bool
Microsoft.AspNetCore.Mvc.ApiBehaviorOptions.SkipStatusCodePages.set -> void
Microsoft.AspNetCore.Mvc.ApplicationModels.SkipStatusCodePagesConvention
Microsoft.AspNetCore.Mvc.ApplicationModels.SkipStatusCodePagesConvention.Apply(Microsoft.AspNetCore.Mvc.ApplicationModels.ActionModel! action) -> void
Microsoft.AspNetCore.Mvc.ApplicationModels.SkipStatusCodePagesConvention.SkipStatusCodePagesConvention() -> void
virtual Microsoft.AspNetCore.Mvc.ApplicationModels.SkipStatusCodePagesConvention.ShouldApply(Microsoft.AspNetCore.Mvc.ApplicationModels.ActionModel! action) -> bool
virtual Microsoft.AspNetCore.Mvc.ControllerBase.Problem(string? detail, string? instance, int? statusCode, string? title, string? type) -> Microsoft.AspNetCore.Mvc.ObjectResult!
virtual Microsoft.AspNetCore.Mvc.ControllerBase.Problem(string? detail = null, string? instance = null, int? statusCode = null, string? title = null, string? type = null, System.Collections.Generic.IDictionary<string!, object?>? extensions = null) -> Microsoft.AspNetCore.Mvc.ObjectResult!
virtual Microsoft.AspNetCore.Mvc.ControllerBase.ValidationProblem(string? detail, string? instance, int? statusCode, string? title, string? type, Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary? modelStateDictionary) -> Microsoft.AspNetCore.Mvc.ActionResult!
Expand Down
Loading