[Fixes #4960] Action results returned from controller actions rendered as json instead of executed
This commit is contained in:
parent
edb5baf81c
commit
998a47d265
|
|
@ -641,7 +641,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal
|
||||||
else if (!_executor.IsMethodAsync)
|
else if (!_executor.IsMethodAsync)
|
||||||
{
|
{
|
||||||
var resultAsObject = _executor.Execute(_controller, arguments);
|
var resultAsObject = _executor.Execute(_controller, arguments);
|
||||||
result = new ObjectResult(resultAsObject)
|
result = resultAsObject as IActionResult ?? new ObjectResult(resultAsObject)
|
||||||
{
|
{
|
||||||
DeclaredType = returnType,
|
DeclaredType = returnType,
|
||||||
};
|
};
|
||||||
|
|
@ -649,7 +649,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal
|
||||||
else if (_executor.TaskGenericType != null)
|
else if (_executor.TaskGenericType != null)
|
||||||
{
|
{
|
||||||
var resultAsObject = await _executor.ExecuteAsync(_controller, arguments);
|
var resultAsObject = await _executor.ExecuteAsync(_controller, arguments);
|
||||||
result = new ObjectResult(resultAsObject)
|
result = resultAsObject as IActionResult ?? new ObjectResult(resultAsObject)
|
||||||
{
|
{
|
||||||
DeclaredType = _executor.TaskGenericType,
|
DeclaredType = _executor.TaskGenericType,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -2356,6 +2356,58 @@ namespace Microsoft.AspNetCore.Mvc.Internal
|
||||||
Assert.IsType<EmptyResult>(result);
|
Assert.IsType<EmptyResult>(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task InvokeAction_AsyncActionWithTaskOfObjectReturnType_AndReturningTaskOfActionResult()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var actionParameters = new Dictionary<string, object> { ["value"] = 3 };
|
||||||
|
IActionResult result = null;
|
||||||
|
|
||||||
|
var filter = new Mock<IActionFilter>(MockBehavior.Strict);
|
||||||
|
filter.Setup(f => f.OnActionExecuting(It.IsAny<ActionExecutingContext>())).Verifiable();
|
||||||
|
filter
|
||||||
|
.Setup(f => f.OnActionExecuted(It.IsAny<ActionExecutedContext>()))
|
||||||
|
.Callback<ActionExecutedContext>(c => result = c.Result);
|
||||||
|
|
||||||
|
var invoker = CreateInvoker(
|
||||||
|
new[] { filter.Object },
|
||||||
|
nameof(TestController.AsyncActionMethodReturningActionResultWithTaskOfObjectAsReturnType),
|
||||||
|
actionParameters);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await invoker.InvokeAsync();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var testResult = Assert.IsType<TestActionResult>(result);
|
||||||
|
Assert.Equal(3, testResult.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task InvokeAction_ActionWithObjectReturnType_AndReturningActionResult()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var actionParameters = new Dictionary<string, object> { ["value"] = 3 };
|
||||||
|
IActionResult result = null;
|
||||||
|
|
||||||
|
var filter = new Mock<IActionFilter>(MockBehavior.Strict);
|
||||||
|
filter.Setup(f => f.OnActionExecuting(It.IsAny<ActionExecutingContext>())).Verifiable();
|
||||||
|
filter
|
||||||
|
.Setup(f => f.OnActionExecuted(It.IsAny<ActionExecutedContext>()))
|
||||||
|
.Callback<ActionExecutedContext>(c => result = c.Result);
|
||||||
|
|
||||||
|
var invoker = CreateInvoker(
|
||||||
|
new[] { filter.Object },
|
||||||
|
nameof(TestController.ActionMethodReturningActionResultWithObjectAsReturnType),
|
||||||
|
actionParameters);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await invoker.InvokeAsync();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var testResult = Assert.IsType<TestActionResult>(result);
|
||||||
|
Assert.Equal(3, testResult.Value);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task InvokeAction_AsyncMethod_ParametersInRandomOrder()
|
public async Task InvokeAction_AsyncMethod_ParametersInRandomOrder()
|
||||||
{
|
{
|
||||||
|
|
@ -2908,6 +2960,16 @@ namespace Microsoft.AspNetCore.Mvc.Internal
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public object ActionMethodReturningActionResultWithObjectAsReturnType(int value = 5)
|
||||||
|
{
|
||||||
|
return new TestActionResult { Value = value };
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<object> AsyncActionMethodReturningActionResultWithTaskOfObjectAsReturnType(int value = 5)
|
||||||
|
{
|
||||||
|
return await Task.FromResult(new TestActionResult { Value = value });
|
||||||
|
}
|
||||||
|
|
||||||
public TestActionResult TestActionMethodWithNullActionResult()
|
public TestActionResult TestActionMethodWithNullActionResult()
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,9 @@ namespace BasicWebSite.Controllers
|
||||||
return View();
|
return View();
|
||||||
}
|
}
|
||||||
|
|
||||||
public IActionResult PlainView()
|
// Keep the return type as object to ensure that we don't
|
||||||
|
// wrap IActionResult instances into ObjectResults.
|
||||||
|
public object PlainView()
|
||||||
{
|
{
|
||||||
return View();
|
return View();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue