diff --git a/src/Mvc/Mvc.Core/src/ControllerBase.cs b/src/Mvc/Mvc.Core/src/ControllerBase.cs index f93974e56ee..52a2846c37f 100644 --- a/src/Mvc/Mvc.Core/src/ControllerBase.cs +++ b/src/Mvc/Mvc.Core/src/ControllerBase.cs @@ -1850,6 +1850,24 @@ public abstract class ControllerBase public virtual ConflictObjectResult Conflict([ActionResultObjectValue] ModelStateDictionary modelState) => new ConflictObjectResult(modelState); + /// + /// Creates an that produces a response. + /// + /// The object to produce a response from. + /// The created for the response. + [NonAction] + public virtual ObjectResult Problem(ProblemDetails problemDetails) + { + ArgumentNullException.ThrowIfNull(problemDetails); + + problemDetails.Status ??= 500; + + return new ObjectResult(problemDetails) + { + StatusCode = problemDetails.Status + }; + } + /// /// Creates an that produces a response. /// diff --git a/src/Mvc/Mvc.Core/src/PublicAPI.Unshipped.txt b/src/Mvc/Mvc.Core/src/PublicAPI.Unshipped.txt index a6374db5adb..59eb483374d 100644 --- a/src/Mvc/Mvc.Core/src/PublicAPI.Unshipped.txt +++ b/src/Mvc/Mvc.Core/src/PublicAPI.Unshipped.txt @@ -29,4 +29,5 @@ virtual Microsoft.AspNetCore.Mvc.ControllerBase.Created() -> Microsoft.AspNetCor *REMOVED*virtual Microsoft.AspNetCore.Mvc.ControllerBase.Created(System.Uri! uri, object? value) -> Microsoft.AspNetCore.Mvc.CreatedResult! virtual Microsoft.AspNetCore.Mvc.ControllerBase.Created(string? uri, object? value) -> Microsoft.AspNetCore.Mvc.CreatedResult! virtual Microsoft.AspNetCore.Mvc.ControllerBase.Created(System.Uri? uri, object? value) -> Microsoft.AspNetCore.Mvc.CreatedResult! +virtual Microsoft.AspNetCore.Mvc.ControllerBase.Problem(Microsoft.AspNetCore.Mvc.ProblemDetails! problemDetails) -> Microsoft.AspNetCore.Mvc.ObjectResult! Microsoft.AspNetCore.Mvc.ProblemDetails.Extensions.set -> void (forwarded, contained in Microsoft.AspNetCore.Http.Abstractions) diff --git a/src/Mvc/Mvc.Core/test/ControllerBaseTest.cs b/src/Mvc/Mvc.Core/test/ControllerBaseTest.cs index e129452e8d4..4fb3e45e4a7 100644 --- a/src/Mvc/Mvc.Core/test/ControllerBaseTest.cs +++ b/src/Mvc/Mvc.Core/test/ControllerBaseTest.cs @@ -2415,6 +2415,45 @@ public class ControllerBaseTest Assert.Equal(400, problemDetails.Status); } + [Fact] + public void ProblemDetails_Object_Works() + { + // Arrange + var problemDetails = new ProblemDetails(); + + var controller = new TestableController(); + + // Act + var actionResult = controller.Problem(problemDetails); + + // Assert + var badRequestResult = Assert.IsType(actionResult); + Assert.Same(problemDetails, Assert.IsType(badRequestResult.Value)); + Assert.Equal(500, actionResult.StatusCode); + Assert.Equal(500, problemDetails.Status); + } + + [Fact] + public void ProblemDetails_Object_UsesPassedInStatusCode() + { + // Arrange + var problemDetails = new ProblemDetails + { + Status = 503 + }; + + var controller = new TestableController(); + + // Act + var actionResult = controller.Problem(problemDetails); + + // Assert + var badRequestResult = Assert.IsType(actionResult); + Assert.Same(problemDetails, Assert.IsType(badRequestResult.Value)); + Assert.Equal(503, actionResult.StatusCode); + Assert.Equal(503, problemDetails.Status); + } + [Fact] public void ProblemDetails_Works() {