问题描述
是否有可能一把umbraco SurfaceController(和UmbracoApiController)
Is it possible to use an async action within an Umbraco SurfaceController (and UmbracoApiController)
我尝试以下code
public async Task< ActionResult> HandleLogin(LoginViewModel model)
{
await Task.Delay(1000);
return PartialView("Login", model);
}
虽然它编译正确时的动作被称为行动似乎只要AWAIT被击中返回,并返回一个字符串
and although it compiled correctly when the action is called the action seems to return as soon as the await is hit, and returns a string
System.Threading.Tasks.Task`1 [System.Web.Mvc.ActionResult]
当然控制器所继承的 SurfaceController 并不知这是什么问题?
the controller of course inherits from SurfaceController and I wonder if this is the problem?
如果这是不可能的,是否有任何变通办法来实现异步操作的行为?
If this is not possible, are there any workarounds to achieve async action behaviour?
任何帮助将受到欢迎!
推荐答案
在一把umbraco的SurfaceControllers最终从获得的 System.Web.Mvc.Controller 的但是他们有自定义操作调用( RenderActionInvoker )设置。
The SurfaceControllers in Umbraco ultimately derive from System.Web.Mvc.Controller However they have custom action invoker (RenderActionInvoker) set.
的 RenderActionInvoker 的距离的 ContollerActionInvoker 的继承。为了处理异步操作,应该不是从的 AsyncContolkerActionInvoker 的派生。的 RenderActionInvoker 的覆盖只有findaction方法,以便从变化的 AsyncContolkerActionInvoker 的推导是很容易。
RenderActionInvoker inherits from ContollerActionInvoker. In order to process async actions it should instead derive from AsyncContolkerActionInvoker. RenderActionInvoker overrides only the findaction method so changing to derive from AsyncContolkerActionInvoker is easy.
在我重新编译Umbraco.Web这种变化,异步操作正常工作。
Once I recompiled Umbraco.Web with this change, async actions worked fine.
而不是重新编译整个项目,我想你可以对每一类指定一个新的actioninvoker
Rather than recompiling the whole project, I guess you could specify a new actioninvoker on each class
public class RenderActionInvokerAsync : System.Web.Mvc.Async.AsyncControllerActionInvoker
{
protected override ActionDescriptor FindAction(ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, string actionName)
{
var ad = base.FindAction(controllerContext, controllerDescriptor, actionName);
if (ad == null)
{
//check if the controller is an instance of IRenderMvcController
if (controllerContext.Controller is IRenderMvcController)
{
return new ReflectedActionDescriptor(
controllerContext.Controller.GetType().GetMethods()
.First(x => x.Name == "Index" &&
x.GetCustomAttributes(typeof(NonActionAttribute), false).Any() == false),
"Index",
controllerDescriptor);
}
}
return ad;
}
}
public class TestController : SurfaceController
{
public TestController() {
this.ActionInvoker = new RenderActionInvokerAsync();
}
public async Task<ActionResult> Test()
{
await Task.Delay(10000);
return PartialView("TestPartial");
}
}
没有测试过,虽然做事的这种方式。
Haven't tested this way of doing things though.
这篇关于异步控制器动作用一把umbraco 7返回字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!