[Fixes #4960] Action results returned from controller actions rendered as json instead of executed
This commit is contained in:
parent
f3f2bcdbb5
commit
7a3f24d49d
|
|
@ -641,7 +641,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal
|
|||
else if (!_executor.IsMethodAsync)
|
||||
{
|
||||
var resultAsObject = _executor.Execute(_controller, arguments);
|
||||
result = new ObjectResult(resultAsObject)
|
||||
result = resultAsObject as IActionResult ?? new ObjectResult(resultAsObject)
|
||||
{
|
||||
DeclaredType = returnType,
|
||||
};
|
||||
|
|
@ -649,7 +649,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal
|
|||
else if (_executor.TaskGenericType != null)
|
||||
{
|
||||
var resultAsObject = await _executor.ExecuteAsync(_controller, arguments);
|
||||
result = new ObjectResult(resultAsObject)
|
||||
result = resultAsObject as IActionResult ?? new ObjectResult(resultAsObject)
|
||||
{
|
||||
DeclaredType = _executor.TaskGenericType,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -2357,6 +2357,58 @@ namespace Microsoft.AspNetCore.Mvc.Internal
|
|||
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]
|
||||
public async Task InvokeAction_AsyncMethod_ParametersInRandomOrder()
|
||||
{
|
||||
|
|
@ -2914,6 +2966,16 @@ namespace Microsoft.AspNetCore.Mvc.Internal
|
|||
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()
|
||||
{
|
||||
return null;
|
||||
|
|
|
|||
|
|
@ -16,7 +16,9 @@ namespace BasicWebSite.Controllers
|
|||
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();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue