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

Con*_*eak 3 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

然后OtherController

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

我尝试了这一点,并在第5行得到了相同的错误:

public class FirstController : Controller{
    public ActionResult Index()
    {
        OtherController OtherController = new OtherController();
        return OtherController.Index();
    }
}
Run Code Online (Sandbox Code Playgroud)

我该如何工作?

nic*_*k_w 6

由于此方法标记为await,因此需要调用的结果。因为您正在等待该调用,所以您的方法也需要标记为。OtherController.Index();asyncFirstController.Indexasync

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

return await OtherController.Index();
Run Code Online (Sandbox Code Playgroud)