jason
jason

Reputation: 7164

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 :

Anonymous function converted to a void returning delegate cannot return a value

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

Can you tell me how I can go to another controller from this controller? Thanks.

Upvotes: 0

Views: 1146

Answers (1)

Robert MacLean
Robert MacLean

Reputation: 39261

Anonymous function converted to a void returning delegate cannot return a value

That line explains it well, you are creating an anonymous function () => which returns void. This maybe confusing, Task.Run returns a Task which isn't void, but it doesn't have a generic type like Task has. However there is a Task.Run which returns Task<TResult> well because

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. Being explicit of the type likely won't resolve this but should help you find the issues, i.e.

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

Some other points on your code that might be worth checking too

Upvotes: 1

Related Questions