Strongly Typed RedirectToAction (Futures) using async Controllers
Asked Answered
W

1

7

having this code it gives me a warning : Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

    public async Task<ActionResult> Details(Guid id)
    {
        var calendarEvent = await service.FindByIdAsync(id);
        if (calendarEvent == null) return RedirectToAction<CalendarController>(c => c.Index());
        var model = new CalendarEventPresentation(calendarEvent);
        ViewData.Model = model;
        return View();
    }

    public async Task<RedirectResult> Create(CalendarEventBindingModel binding)
    {
        var model = service.Create(binding);
        await context.SaveChangesAsync();
        return this.RedirectToAction<CalendarController>(c => c.Details(model.CalendarEventID));
    }

if I do add the await operator I get :

Error CS4034 The 'await' operator can only be used within an async lambda expression. Consider marking this lambda expression with the 'async' modifier.

if I add the async modifier like this :

return this.RedirectToAction<CalendarController>(async c => await c.Details(model.CalendarEventID));

the error is Error CS1989 Async lambda expressions cannot be converted to expression trees

So how can I use strongly typed RedirectToAction (I'm using MVC Futures) with async controllers?

Wortman answered 4/2, 2015 at 12:38 Comment(3)
I guess you need to write an extension which will accept Func<Task<ViewResult>> or whatever and then translate it into futures' version (non-async) of expression.Briticism
mm.. i can't make it to work :(Wortman
Since the expression itself is not executed, I don't think is problematic - other than the warnings emitted, which are really annoying. Disabling them with #pragma seems the wrong approach to fix it though. Let's hope someone smarter comes along and fixes this. The following link seems somewhat related: #24241202Peristome
D
3

I have figured this out!

I have a base controller and there I have an extension method for async redirects

protected ActionResult RedirectToAsyncAction<TController>(Expression<Func<TController, Task<ActionResult>>> action)
        where TController : Controller
    {
        Expression<Action<TController>> convertedFuncToAction = Expression.Lambda<Action<TController>>(action.Body, action.Parameters.First());
        return ControllerExtensions.RedirectToAction(this, convertedFuncToAction);
    }

this will prevent warnings. Then you can just call RedirectToAsyncAction from your Controller.

public ActionResult MyAction()
    {
       // Your code
        return RedirectToAsyncAction<MyController>(c => c.MyAsyncAction(params,..)); // no warnings here
    }
Demetri answered 2/8, 2018 at 7:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.