Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
/MvcPublic archive

Commit998a47d

Browse files
javiercnryanbrandenburg
authored andcommitted
[Fixes#4960] Action results returned from controller actions rendered as json instead of executed
1 parentedb5baf commit998a47d

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

‎src/Microsoft.AspNetCore.Mvc.Core/Internal/ControllerActionInvoker.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,15 +641,15 @@ private async Task InvokeActionFilterAsync()
641641
elseif(!_executor.IsMethodAsync)
642642
{
643643
varresultAsObject=_executor.Execute(_controller,arguments);
644-
result=newObjectResult(resultAsObject)
644+
result=resultAsObjectasIActionResult??newObjectResult(resultAsObject)
645645
{
646646
DeclaredType=returnType,
647647
};
648648
}
649649
elseif(_executor.TaskGenericType!=null)
650650
{
651651
varresultAsObject=await_executor.ExecuteAsync(_controller,arguments);
652-
result=newObjectResult(resultAsObject)
652+
result=resultAsObjectasIActionResult??newObjectResult(resultAsObject)
653653
{
654654
DeclaredType=_executor.TaskGenericType,
655655
};

‎test/Microsoft.AspNetCore.Mvc.Core.Test/Internal/ControllerActionInvokerTest.cs‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2356,6 +2356,58 @@ public async Task InvokeAction_AsyncAction_ReturningUnwrappedTask()
23562356
Assert.IsType<EmptyResult>(result);
23572357
}
23582358

2359+
[Fact]
2360+
publicasyncTaskInvokeAction_AsyncActionWithTaskOfObjectReturnType_AndReturningTaskOfActionResult()
2361+
{
2362+
// Arrange
2363+
varactionParameters=newDictionary<string,object>{["value"]=3};
2364+
IActionResultresult=null;
2365+
2366+
varfilter=newMock<IActionFilter>(MockBehavior.Strict);
2367+
filter.Setup(f=>f.OnActionExecuting(It.IsAny<ActionExecutingContext>())).Verifiable();
2368+
filter
2369+
.Setup(f=>f.OnActionExecuted(It.IsAny<ActionExecutedContext>()))
2370+
.Callback<ActionExecutedContext>(c=>result=c.Result);
2371+
2372+
varinvoker=CreateInvoker(
2373+
new[]{filter.Object},
2374+
nameof(TestController.AsyncActionMethodReturningActionResultWithTaskOfObjectAsReturnType),
2375+
actionParameters);
2376+
2377+
// Act
2378+
awaitinvoker.InvokeAsync();
2379+
2380+
// Assert
2381+
vartestResult=Assert.IsType<TestActionResult>(result);
2382+
Assert.Equal(3,testResult.Value);
2383+
}
2384+
2385+
[Fact]
2386+
publicasyncTaskInvokeAction_ActionWithObjectReturnType_AndReturningActionResult()
2387+
{
2388+
// Arrange
2389+
varactionParameters=newDictionary<string,object>{["value"]=3};
2390+
IActionResultresult=null;
2391+
2392+
varfilter=newMock<IActionFilter>(MockBehavior.Strict);
2393+
filter.Setup(f=>f.OnActionExecuting(It.IsAny<ActionExecutingContext>())).Verifiable();
2394+
filter
2395+
.Setup(f=>f.OnActionExecuted(It.IsAny<ActionExecutedContext>()))
2396+
.Callback<ActionExecutedContext>(c=>result=c.Result);
2397+
2398+
varinvoker=CreateInvoker(
2399+
new[]{filter.Object},
2400+
nameof(TestController.ActionMethodReturningActionResultWithObjectAsReturnType),
2401+
actionParameters);
2402+
2403+
// Act
2404+
awaitinvoker.InvokeAsync();
2405+
2406+
// Assert
2407+
vartestResult=Assert.IsType<TestActionResult>(result);
2408+
Assert.Equal(3,testResult.Value);
2409+
}
2410+
23592411
[Fact]
23602412
publicasyncTaskInvokeAction_AsyncMethod_ParametersInRandomOrder()
23612413
{
@@ -2908,6 +2960,16 @@ public IActionResult ActionMethodWithNullActionResult()
29082960
returnnull;
29092961
}
29102962

2963+
publicobjectActionMethodReturningActionResultWithObjectAsReturnType(intvalue=5)
2964+
{
2965+
returnnewTestActionResult{Value=value};
2966+
}
2967+
2968+
publicasyncTask<object>AsyncActionMethodReturningActionResultWithTaskOfObjectAsReturnType(intvalue=5)
2969+
{
2970+
returnawaitTask.FromResult(newTestActionResult{Value=value});
2971+
}
2972+
29112973
publicTestActionResultTestActionMethodWithNullActionResult()
29122974
{
29132975
returnnull;

‎test/WebSites/BasicWebSite/Controllers/HomeController.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ public IActionResult Index()
1616
returnView();
1717
}
1818

19-
publicIActionResultPlainView()
19+
// Keep the return type as object to ensure that we don't
20+
// wrap IActionResult instances into ObjectResults.
21+
publicobjectPlainView()
2022
{
2123
returnView();
2224
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp