Add IStatusCodeActionResult (#8265)

* Add IStatusCodeActionResult
* Add unit test for explicitly implemented property on StatusCodeResult
This commit is contained in:
Kristian Hellang 2018-08-14 00:39:20 +02:00 committed by Pranav K
parent 93d9c11778
commit 2421ae8c83
9 changed files with 42 additions and 7 deletions

View File

@ -9,7 +9,7 @@ using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.AspNetCore.Mvc
{
public class ContentResult : ActionResult
public class ContentResult : ActionResult, IStatusCodeActionResult
{
/// <summary>
/// Gets or set the content representing the body of the response.

View File

@ -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
{
/// <summary>
/// Represents an <see cref="IActionResult"/> that when executed will
/// produce an HTTP response with the specified <see cref="StatusCode"/>.
/// </summary>
public interface IStatusCodeActionResult : IActionResult
{
/// <summary>
/// Gets or sets the HTTP status code.
/// </summary>
int? StatusCode { get; }
}
}

View File

@ -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)
{

View File

@ -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 <see cref="ActionResult"/> that when executed will
/// produce an HTTP response with the given response status code.
/// </summary>
public class StatusCodeResult : ActionResult
public class StatusCodeResult : ActionResult, IStatusCodeActionResult
{
/// <summary>
/// Initializes a new instance of the <see cref="StatusCodeResult"/> class
@ -29,6 +30,8 @@ namespace Microsoft.AspNetCore.Mvc
/// </summary>
public int StatusCode { get; }
int? IStatusCodeActionResult.StatusCode => StatusCode;
/// <inheritdoc />
public override void ExecuteResult(ActionContext context)
{

View File

@ -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
/// <summary>
/// An action result which formats the given object as JSON.
/// </summary>
public class JsonResult : ActionResult
public class JsonResult : ActionResult, IStatusCodeActionResult
{
/// <summary>
/// Creates a new <see cref="JsonResult"/> with the given <paramref name="value"/>.

View File

@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Mvc
/// <summary>
/// Represents an <see cref="ActionResult"/> that renders a partial view to the response.
/// </summary>
public class PartialViewResult : ActionResult
public class PartialViewResult : ActionResult, IStatusCodeActionResult
{
/// <summary>
/// Gets or sets the HTTP status code.

View File

@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.Mvc
/// <summary>
/// An <see cref="IActionResult"/> which renders a view component to the response.
/// </summary>
public class ViewComponentResult : ActionResult
public class ViewComponentResult : ActionResult, IStatusCodeActionResult
{
/// <summary>
/// Gets or sets the arguments provided to the view component.

View File

@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Mvc
/// <summary>
/// Represents an <see cref="ActionResult"/> that renders a view to the response.
/// </summary>
public class ViewResult : ActionResult
public class ViewResult : ActionResult, IStatusCodeActionResult
{
/// <summary>
/// Gets or sets the HTTP status code.

View File

@ -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();