Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Return EmptyResult for actions with void or Task return type #2687

Merged
merged 1 commit into from
Jun 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/Microsoft.AspNet.Mvc.Core/ControllerActionInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,7 @@ internal static IActionResult CreateActionResult([NotNull] Type declaredReturnTy
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1920,17 +1920,13 @@ public void CreateActionResult_NullActionResultReturnValueThrows()
[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
Expand Down
5 changes: 3 additions & 2 deletions test/Microsoft.AspNet.Mvc.FunctionalTests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public async Task CanReturn_ResultsWithoutContent()
}

[Fact]
public async Task ReturningTaskFromAction_ProducesNoContentResult()
public async Task ReturningTaskFromAction_ProducesEmptyResult()
{
// Arrange
var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
Expand All @@ -139,8 +139,9 @@ public async Task ReturningTaskFromAction_ProducesNoContentResult()
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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public async Task JsonOutputFormatter_ForNonStringValue_GetsSelected(string acti
[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);
Expand All @@ -74,7 +74,7 @@ public async Task NoContentFormatter_ForVoidAndTaskReturnType_GetsSelectedAndWri
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);
}

Expand Down