Skip to content

Commit

Permalink
Add IStatusCodeActionResult (aspnet#8265)
Browse files Browse the repository at this point in the history
* Add IStatusCodeActionResult
* Add unit test for explicitly implemented property on StatusCodeResult
  • Loading branch information
khellang authored and pranavkm committed Aug 13, 2018
1 parent 93d9c11 commit 2421ae8
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Microsoft.AspNetCore.Mvc.Core/ContentResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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; }
}
}
2 changes: 1 addition & 1 deletion src/Microsoft.AspNetCore.Mvc.Core/ObjectResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Microsoft.AspNetCore.Mvc
{
public class ObjectResult : ActionResult
public class ObjectResult : ActionResult, IStatusCodeActionResult
{
public ObjectResult(object value)
{
Expand Down
5 changes: 4 additions & 1 deletion src/Microsoft.AspNetCore.Mvc.Core/StatusCodeResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -29,6 +30,8 @@ public StatusCodeResult(int statusCode)
/// </summary>
public int StatusCode { get; }

int? IStatusCodeActionResult.StatusCode => StatusCode;

/// <inheritdoc />
public override void ExecuteResult(ActionContext context)
{
Expand Down
3 changes: 2 additions & 1 deletion src/Microsoft.AspNetCore.Mvc.Formatters.Json/JsonResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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"/>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -32,6 +33,19 @@ public void HttpStatusCodeResult_ExecuteResultSetsResponseStatusCode()
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();
Expand Down

0 comments on commit 2421ae8

Please sign in to comment.