[Fixes #2409] Return EmptyResult for void actions
This commit is contained in:
parent
31aa51ba49
commit
a3c593bda9
|
|
@ -108,11 +108,7 @@ namespace Microsoft.AspNet.Mvc.Core
|
||||||
if (declaredReturnType == typeof(void) ||
|
if (declaredReturnType == typeof(void) ||
|
||||||
declaredReturnType == typeof(Task))
|
declaredReturnType == typeof(Task))
|
||||||
{
|
{
|
||||||
return new ObjectResult(null)
|
return new EmptyResult();
|
||||||
{
|
|
||||||
// Treat the declared type as void, which is the unwrapped type for Task.
|
|
||||||
DeclaredType = typeof(void)
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unwrap potential Task<T> types.
|
// Unwrap potential Task<T> types.
|
||||||
|
|
|
||||||
|
|
@ -1920,17 +1920,13 @@ namespace Microsoft.AspNet.Mvc
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData(typeof(void))]
|
[InlineData(typeof(void))]
|
||||||
[InlineData(typeof(Task))]
|
[InlineData(typeof(Task))]
|
||||||
public void CreateActionResult_Types_ReturnsObjectResultForTaskAndVoidReturnTypes(Type type)
|
public void CreateActionResult_Types_ReturnsEmptyResultForTaskAndVoidReturnTypes(Type type)
|
||||||
{
|
{
|
||||||
// Arrange & Act
|
// Arrange & Act
|
||||||
var result = ControllerActionInvoker.CreateActionResult(type, null);
|
var result = ControllerActionInvoker.CreateActionResult(type, null);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
var objectResult = Assert.IsType<ObjectResult>(result);
|
Assert.IsType<EmptyResult>(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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IEnumerable<object[]> CreateActionResult_ReturnsObjectContentResultData
|
public static IEnumerable<object[]> CreateActionResult_ReturnsObjectContentResultData
|
||||||
|
|
|
||||||
|
|
@ -129,7 +129,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task ReturningTaskFromAction_ProducesNoContentResult()
|
public async Task ReturningTaskFromAction_ProducesEmptyResult()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
|
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");
|
var response = await client.GetAsync("http://localhost/Home/ActionReturningTask");
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
Assert.Equal("Hello, World!", Assert.Single(response.Headers.GetValues("Message")));
|
Assert.Equal("Hello, World!", Assert.Single(response.Headers.GetValues("Message")));
|
||||||
|
Assert.Empty(await response.Content.ReadAsStringAsync());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
|
||||||
|
|
@ -60,7 +60,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData("ReturnTask")]
|
[InlineData("ReturnTask")]
|
||||||
[InlineData("ReturnVoid")]
|
[InlineData("ReturnVoid")]
|
||||||
public async Task NoContentFormatter_ForVoidAndTaskReturnType_GetsSelectedAndWritesResponse(string actionName)
|
public async Task NoContentFormatter_ForVoidAndTaskReturnType_DoesNotRun(string actionName)
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
|
var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
|
||||||
|
|
@ -74,7 +74,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
var body = await response.Content.ReadAsStringAsync();
|
var body = await response.Content.ReadAsStringAsync();
|
||||||
// Response body is empty instead of null.
|
// Response body is empty instead of null.
|
||||||
Assert.Empty(body);
|
Assert.Empty(body);
|
||||||
Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
Assert.Equal(0, response.Content.Headers.ContentLength);
|
Assert.Equal(0, response.Content.Headers.ContentLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue