错误在下面代码的第 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();
}
}
我如何让它工作?
最佳答案
您需要等待
调用OtherController.Index();
的结果,因为此方法被标记为async
。因为您正在等待该调用,所以您的 FirstController.Index
方法也需要标记为 async
。
因此,在您的 FirstController.Index
方法中,您将拥有:
return await OtherController.Index();
关于c# - 无法将类型 'System.Threading.Tasks.Task<System.Web.Mvc.ActionResult>' 隐式转换为 'System.Web.Mvc.ActionResult',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26171775/