From 2421ae8c833b7cbb1991e43c8dda9632f9e2d61a Mon Sep 17 00:00:00 2001 From: Kristian Hellang Date: Tue, 14 Aug 2018 00:39:20 +0200 Subject: [PATCH] Add IStatusCodeActionResult (#8265) * Add IStatusCodeActionResult * Add unit test for explicitly implemented property on StatusCodeResult --- .../ContentResult.cs | 2 +- .../Infrastructure/IStatusCodeActionResult.cs | 17 +++++++++++++++++ .../ObjectResult.cs | 2 +- .../StatusCodeResult.cs | 5 ++++- .../JsonResult.cs | 3 ++- .../PartialViewResult.cs | 2 +- .../ViewComponentResult.cs | 2 +- .../ViewResult.cs | 2 +- .../HttpStatusCodeResultTests.cs | 14 ++++++++++++++ 9 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/IStatusCodeActionResult.cs diff --git a/src/Microsoft.AspNetCore.Mvc.Core/ContentResult.cs b/src/Microsoft.AspNetCore.Mvc.Core/ContentResult.cs index 65ad59bf47..29c9ddab2d 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/ContentResult.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/ContentResult.cs @@ -9,7 +9,7 @@ using Microsoft.Extensions.DependencyInjection; namespace Microsoft.AspNetCore.Mvc { - public class ContentResult : ActionResult + public class ContentResult : ActionResult, IStatusCodeActionResult { /// /// Gets or set the content representing the body of the response. diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/IStatusCodeActionResult.cs b/src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/IStatusCodeActionResult.cs new file mode 100644 index 0000000000..a8eabf652d --- /dev/null +++ b/src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/IStatusCodeActionResult.cs @@ -0,0 +1,17 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +namespace Microsoft.AspNetCore.Mvc.Infrastructure +{ + /// + /// Represents an that when executed will + /// produce an HTTP response with the specified . + /// + public interface IStatusCodeActionResult : IActionResult + { + /// + /// Gets or sets the HTTP status code. + /// + int? StatusCode { get; } + } +} diff --git a/src/Microsoft.AspNetCore.Mvc.Core/ObjectResult.cs b/src/Microsoft.AspNetCore.Mvc.Core/ObjectResult.cs index 8e850a363c..e81dbccfa1 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/ObjectResult.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/ObjectResult.cs @@ -9,7 +9,7 @@ using Microsoft.Extensions.DependencyInjection; namespace Microsoft.AspNetCore.Mvc { - public class ObjectResult : ActionResult + public class ObjectResult : ActionResult, IStatusCodeActionResult { public ObjectResult(object value) { diff --git a/src/Microsoft.AspNetCore.Mvc.Core/StatusCodeResult.cs b/src/Microsoft.AspNetCore.Mvc.Core/StatusCodeResult.cs index 8db690c94e..305d279372 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/StatusCodeResult.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/StatusCodeResult.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using Microsoft.AspNetCore.Mvc.Infrastructure; using Microsoft.AspNetCore.Mvc.Internal; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -12,7 +13,7 @@ namespace Microsoft.AspNetCore.Mvc /// Represents an that when executed will /// produce an HTTP response with the given response status code. /// - public class StatusCodeResult : ActionResult + public class StatusCodeResult : ActionResult, IStatusCodeActionResult { /// /// Initializes a new instance of the class @@ -29,6 +30,8 @@ namespace Microsoft.AspNetCore.Mvc /// public int StatusCode { get; } + int? IStatusCodeActionResult.StatusCode => StatusCode; + /// public override void ExecuteResult(ActionContext context) { diff --git a/src/Microsoft.AspNetCore.Mvc.Formatters.Json/JsonResult.cs b/src/Microsoft.AspNetCore.Mvc.Formatters.Json/JsonResult.cs index 4380224057..a4a576a83f 100644 --- a/src/Microsoft.AspNetCore.Mvc.Formatters.Json/JsonResult.cs +++ b/src/Microsoft.AspNetCore.Mvc.Formatters.Json/JsonResult.cs @@ -4,6 +4,7 @@ using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.Formatters.Json.Internal; +using Microsoft.AspNetCore.Mvc.Infrastructure; using Microsoft.Extensions.DependencyInjection; using Newtonsoft.Json; @@ -12,7 +13,7 @@ namespace Microsoft.AspNetCore.Mvc /// /// An action result which formats the given object as JSON. /// - public class JsonResult : ActionResult + public class JsonResult : ActionResult, IStatusCodeActionResult { /// /// Creates a new with the given . diff --git a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/PartialViewResult.cs b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/PartialViewResult.cs index e9f2763057..56b0f80139 100644 --- a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/PartialViewResult.cs +++ b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/PartialViewResult.cs @@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Mvc /// /// Represents an that renders a partial view to the response. /// - public class PartialViewResult : ActionResult + public class PartialViewResult : ActionResult, IStatusCodeActionResult { /// /// Gets or sets the HTTP status code. diff --git a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponentResult.cs b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponentResult.cs index 898f676067..cbdb85574c 100644 --- a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponentResult.cs +++ b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponentResult.cs @@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.Mvc /// /// An which renders a view component to the response. /// - public class ViewComponentResult : ActionResult + public class ViewComponentResult : ActionResult, IStatusCodeActionResult { /// /// Gets or sets the arguments provided to the view component. diff --git a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewResult.cs b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewResult.cs index c98562fc9e..5534ec8666 100644 --- a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewResult.cs +++ b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewResult.cs @@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Mvc /// /// Represents an that renders a view to the response. /// - public class ViewResult : ActionResult + public class ViewResult : ActionResult, IStatusCodeActionResult { /// /// Gets or sets the HTTP status code. diff --git a/test/Microsoft.AspNetCore.Mvc.Core.Test/HttpStatusCodeResultTests.cs b/test/Microsoft.AspNetCore.Mvc.Core.Test/HttpStatusCodeResultTests.cs index e69fabaf5e..48634917fb 100644 --- a/test/Microsoft.AspNetCore.Mvc.Core.Test/HttpStatusCodeResultTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.Core.Test/HttpStatusCodeResultTests.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.Abstractions; +using Microsoft.AspNetCore.Mvc.Infrastructure; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -32,6 +33,19 @@ namespace Microsoft.AspNetCore.Mvc Assert.Equal(StatusCodes.Status404NotFound, httpContext.Response.StatusCode); } + [Fact] + public void HttpStatusCodeResult_ReturnsCorrectStatusCodeAsIStatusCodeActionResult() + { + // Arrange + var result = new StatusCodeResult(StatusCodes.Status404NotFound); + + // Act + var statusResult = result as IStatusCodeActionResult; + + // Assert + Assert.Equal(StatusCodes.Status404NotFound, statusResult?.StatusCode); + } + private static IServiceCollection CreateServices() { var services = new ServiceCollection();