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

标签 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();
    }
}

我如何让它工作?

最佳答案

您需要等待调用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/

相关文章:

java - 调用 wait() 后有返回值是什么意思?

python 子进程意外退出,退出代码为-9

c# - 创建接口(interface)子类的实例

c# - InvalidOperationException : Multiple handlers matched. 以下处理程序匹配路由数据并满足所有约束:

c# - MVVM模式的哪一部分负责数据网格的分组

javascript - 在 mvc4 中访问 javascript 中的临时数据

c# - 在 linq 上使用 where all

asp.net - 如何以编程方式打印多页?

C++11 std::threads 并等待线程完成

C# 正则表达式不包括一个字符 (!)