无法将类型'System.Threading.Tasks.Task <system.web.mvc.actionresult>'隐式转换为'System.Web.Mvc.ActionResult'</system.web.mvc.actionresult>

时间:2014-10-03 00:45:20

标签: c# asp.net-mvc multithreading asp.net-mvc-4

错误发生在以下代码的第5行:

public class FirstController : Controller{
    public async Task<ActionResult> Index()
    {
        OtherController OtherController = new OtherController();
        return OtherController.Index();
    }
}

然后是OtherController

public class OtherController : Controller{
    public async Task<ActionResult> Index()
    {            
        //Tasks...
        //await...
        return View();
    }   
}

我试过这个并在第5行得到同样的错误:

public class FirstController : Controller{
    public ActionResult Index()
    {
        OtherController OtherController = new OtherController();
        return OtherController.Index();
    }
}

如何让它发挥作用?

1 个答案:

答案 0 :(得分:5)

您需要await调用OtherController.Index();的结果,因为此方法已标记为async。由于您正在等待该通话,因此您的FirstController.Index方法也需要标记为async

在您的FirstController.Index方法中,您将拥有:

return await OtherController.Index();
相关问题