简体   繁体   English

如何在异步任务中使用return

[英]How to use return in async Task

I have this method in my controller : 我的控制器中有此方法:

    public async Task<ActionResult> GetDetails(Query query)
    {
        var son = await Task.Run(() =>
        {
            if(query.Export)
            {
               return RedirectToAction("GetDetails", "GridToolController");
            }

                if (!query.Export)
                {
                    db.Configuration.AutoDetectChangesEnabled = false;
                }
         }
    }

As you can see, I want to go to another controller, but when I write return, It gives these errors : 如您所见,我想转到另一个控制器,但是当我写return时,它给出了以下错误:

Anonymous function converted to a void returning delegate cannot return a value 匿名函数转换为void返回的委托无法返回值

Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type 无法将lambda表达式转换为预期的委托类型,因为该块中的某些返回类型不能隐式转换为委托返回类型

Can you tell me how I can go to another controller from this controller? 您能告诉我如何从该控制器转到另一个控制器吗? Thanks. 谢谢。

Anonymous function converted to a void returning delegate cannot return a value 匿名函数转换为void返回的委托无法返回值

That line explains it well, you are creating an anonymous function () => which returns void. 该行很好地说明了这一点,您正在创建一个匿名函数() => ,该函数返回void。 This maybe confusing, Task.Run returns a Task which isn't void, but it doesn't have a generic type like Task has. 这可能令人困惑, Task.Run返回一个Task ,它不是无效的,但是没有Task所具有的泛型类型。 However there is a Task.Run which returns Task<TResult> well because 但是有一个Task.Run可以很好地返回Task<TResult>

some of the return types in the block are not implicitly convertible to the delegate return type 块中的某些返回类型不能隐式转换为委托返回类型

Guessing in some places in that code you are doing other things that are not returning a RedirectToAction . 在该代码中的某些地方猜测,您正在做其他未返回RedirectToAction事情。 Being explicit of the type likely won't resolve this but should help you find the issues, ie 明确类型可能无法解决此问题,但应该可以帮助您发现问题,即

var son = await Task.Run<ActionResult>(() =>

Some other points on your code that might be worth checking too 您代码上的其他一些点可能也值得检查

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM