[Fixes #2409] Return EmptyResult for void actions

This commit is contained in:
Ajay Bhargav Baaskaran 2015-06-10 16:21:31 -07:00
parent 31aa51ba49
commit a3c593bda9
4 changed files with 8 additions and 15 deletions

View File

@ -108,11 +108,7 @@ namespace Microsoft.AspNet.Mvc.Core
if (declaredReturnType == typeof(void) ||
declaredReturnType == typeof(Task))
{
return new ObjectResult(null)
{
// Treat the declared type as void, which is the unwrapped type for Task.
DeclaredType = typeof(void)
};
return new EmptyResult();
}
// Unwrap potential Task<T> types.

View File

@ -1920,17 +1920,13 @@ namespace Microsoft.AspNet.Mvc
[Theory]
[InlineData(typeof(void))]
[InlineData(typeof(Task))]
public void CreateActionResult_Types_ReturnsObjectResultForTaskAndVoidReturnTypes(Type type)
public void CreateActionResult_Types_ReturnsEmptyResultForTaskAndVoidReturnTypes(Type type)
{
// Arrange & Act
var result = ControllerActionInvoker.CreateActionResult(type, null);
// Assert
var objectResult = Assert.IsType<ObjectResult>(result);
// Since we unwrap the Task type to void, the expected type will always be void.
Assert.Equal(typeof(void), objectResult.DeclaredType);
Assert.Null(objectResult.Value);
Assert.IsType<EmptyResult>(result);
}
public static IEnumerable<object[]> CreateActionResult_ReturnsObjectContentResultData

View File

@ -129,7 +129,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
}
[Fact]
public async Task ReturningTaskFromAction_ProducesNoContentResult()
public async Task ReturningTaskFromAction_ProducesEmptyResult()
{
// Arrange
var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
@ -139,8 +139,9 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
var response = await client.GetAsync("http://localhost/Home/ActionReturningTask");
// Assert
Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("Hello, World!", Assert.Single(response.Headers.GetValues("Message")));
Assert.Empty(await response.Content.ReadAsStringAsync());
}
[Fact]

View File

@ -60,7 +60,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
[Theory]
[InlineData("ReturnTask")]
[InlineData("ReturnVoid")]
public async Task NoContentFormatter_ForVoidAndTaskReturnType_GetsSelectedAndWritesResponse(string actionName)
public async Task NoContentFormatter_ForVoidAndTaskReturnType_DoesNotRun(string actionName)
{
// Arrange
var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
@ -74,7 +74,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
var body = await response.Content.ReadAsStringAsync();
// Response body is empty instead of null.
Assert.Empty(body);
Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(0, response.Content.Headers.ContentLength);
}