[Issue #527] Revive common ActionResults - Part 1.

1. Added HttpNotFound() to Controller.
2. Updated HttpStatusCodeResult to expose the StatusCode as a property.
3. Added unit tests for HttpNotFound() and for HttpStatusCodeResult.
4. Updated the MvcSample to add an action that uses HttpNotFound().
5. Brought back HttpNotFoundResult and added unit tests for it.
This commit is contained in:
Javier Calvarro Nelson 2014-07-20 15:56:45 -07:00
parent cd0285183e
commit b0d52f73fd
9 changed files with 116 additions and 4 deletions

View File

@ -29,6 +29,11 @@ namespace MvcSample.Web
return View();
}
public ActionResult NotFound()
{
return HttpNotFound();
}
/// <summary>
/// Action that shows metadata when model is <c>null</c>.
/// </summary>

View File

@ -0,0 +1,19 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.AspNet.Mvc
{
/// <summary>
/// Represents an <see cref="HttpStatusCodeResult"/> that when
/// executed will produce a Not Found (404) response.
/// </summary>
public class HttpNotFoundResult : HttpStatusCodeResult
{
/// <summary>
/// Creates a new <see cref="HttpNotFoundResult"/> instance.
/// </summary>
public HttpNotFoundResult() : base(404)
{
}
}
}

View File

@ -2,21 +2,35 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
namespace Microsoft.AspNet.Mvc
{
/// <summary>
/// Represents an <see cref="ActionResult"/> that when executed will
/// produce an HTTP response with the given response status code.
/// </summary>
public class HttpStatusCodeResult : ActionResult
{
private int _statusCode;
/// <summary>
/// Initializes a new instance of the <see cref="HttpStatusCodeResult"/> class
/// with the given <paramref name="statusCode"/>.
/// </summary>
/// <param name="statusCode">The HTTP status code of the response.</param>
public HttpStatusCodeResult(int statusCode)
{
_statusCode = statusCode;
StatusCode = statusCode;
}
/// <summary>
/// Gets the HTTP status code.
/// </summary>
public int StatusCode { get; private set; }
/// <inheritdoc />
public override void ExecuteResult([NotNull] ActionContext context)
{
context.HttpContext.Response.StatusCode = _statusCode;
context.HttpContext.Response.StatusCode = StatusCode;
}
}
}

View File

@ -415,6 +415,16 @@ namespace Microsoft.AspNet.Mvc
return new RedirectToRouteResult(Url, routeName, routeValues, permanent: true);
}
/// <summary>
/// Creates an <see cref="HttpNotFoundResult"/> that produces a Not Found (404) response.
/// </summary>
/// <returns>The created <see cref="HttpNotFoundResult"/> for the response.</returns>
[NonAction]
public virtual HttpNotFoundResult HttpNotFound()
{
return new HttpNotFoundResult();
}
/// <summary>
/// Called before the action method is invoked.
/// </summary>

View File

@ -29,6 +29,7 @@
<Compile Include="ActionDescriptor.cs" />
<Compile Include="ActionDescriptorProviderContext.cs" />
<Compile Include="ActionDescriptorsCollection.cs" />
<Compile Include="ActionResults\HttpNotFoundResult.cs" />
<Compile Include="OptionDescriptors\DefaultModelBinderProvider.cs" />
<Compile Include="OptionDescriptors\DefaultValueProviderFactoryProvider.cs" />
<Compile Include="OptionDescriptors\DefaultViewEngineProvider.cs" />

View File

@ -0,0 +1,17 @@
using Xunit;
namespace Microsoft.AspNet.Mvc
{
public class HttpNotFoundResultTests
{
[Fact]
public void HttpNotFoundResult_InitializesStatusCode()
{
// Arrange & act
var notFound = new HttpNotFoundResult();
// Assert
Assert.Equal(404, notFound.StatusCode);
}
}
}

View File

@ -0,0 +1,30 @@
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNet.PipelineCore;
using Microsoft.AspNet.Routing;
using Xunit;
namespace Microsoft.AspNet.Mvc
{
public class HttpStatusCodeResultTests
{
[Fact]
public void HttpStatusCodeResult_ExecuteResultSetsResponseStatusCode()
{
// Arrange
var result = new HttpStatusCodeResult(404);
var httpContext = new DefaultHttpContext();
var routeData = new RouteData();
var actionDescriptor = new ActionDescriptor();
var context = new ActionContext(httpContext, routeData, actionDescriptor);
// Act
result.ExecuteResult(context);
// Assert
Assert.Equal(404, httpContext.Response.StatusCode);
}
}
}

View File

@ -357,6 +357,20 @@ namespace Microsoft.AspNet.Mvc.Test
Assert.Equal(TypeHelper.ObjectToDictionary(routeValues), resultPermanent.RouteValues);
}
[Fact]
public void HttpNotFound_SetsStatusCode()
{
// Arrange
var controller = new Controller();
// Act
var result = controller.HttpNotFound();
// Assert
Assert.IsType<HttpNotFoundResult>(result);
Assert.Equal(404, result.StatusCode);
}
[Theory]
[MemberData("PublicNormalMethodsFromController")]
public void NonActionAttribute_IsOnEveryPublicNormalMethodFromController(MethodInfo method)

View File

@ -23,6 +23,8 @@
<ItemGroup>
<Compile Include="ActionAttributeTests.cs" />
<Compile Include="ActionExecutorTests.cs" />
<Compile Include="ActionResults\HttpNotFoundResultTests.cs" />
<Compile Include="ActionResults\HttpStatusCodeResultTests.cs" />
<Compile Include="ActionResults\ObjectContentResultTests.cs" />
<Compile Include="ActionResults\ChallengeResultTest.cs" />
<Compile Include="ActionResults\RedirectToActionResultTest.cs" />