RedirectToAction with Task<ActionResult>

Feb 27, 2014 at 10:57 AM
Hi, I'm using async in my Controllers and it seems that T4MVC doesn't have full support for it.

For example, I want to do a RedirectToAction to an async Task<Result> but the RedirectToAction built it t4mvc only allows an ActionResult as a parameter.

I've added this method to MyBaseController:
        protected RedirectToRouteResult RedirectToAction(Task<ActionResult> taskResult)
        {
            var callInfo = taskResult.Result.GetT4MVCResult();
            return RedirectToRoute(callInfo.RouteValueDictionary);
        }
Am I doing ok?

Thanks!
Coordinator
Feb 27, 2014 at 6:23 PM
All you should have to so is add .Result to the T4MVC call (see this thread). So your helper is fine if you don't want to see .Result everywhere. I guess we could auto generate such helper.

BTW, you can probably simplify it to be just:
    protected RedirectToRouteResult RedirectToAction(Task<ActionResult> taskResult)
    {
        return RedirectToAction(taskResult.Result);
    }
Mar 1, 2014 at 10:51 AM
Hi!

Thanks for your quick response.

I know I could use ".Result", however with the proposed helper it's quite easy to quickly switch between async and traditional-sync ActionResults and it's also easy migrating from a an old sync-based MVC web with a lot of "RedirectToAction's".

On the other hand, I couldn't use your simplified method because I've placed it in MyBaseController (abstract) where I have no access to RedirectToAction(ActionResult).

I've modified my t4mvc.tt with both "RedirectToAction(Task<ActionResult> taskResult)" and "RedirectToActionPermanent(Task<ActionResult> taskResult)" methods, but I really don't want to have a modified non-offical t4mvc.tt

Again thank you very much for your attention [and sorry for my English).
Coordinator
Mar 1, 2014 at 9:36 PM
Feel free to send a PR and we'll get that in. Might need to fully qualify the types to make sure that Task<T> can be resolved when people are not using async.
Coordinator
Mar 3, 2014 at 1:38 AM
I made the change in 3.8.0. Can you please verify that it works well for you?
Mar 3, 2014 at 8:11 AM
Hi!!

Awesome!!! It works perfect ;)

You didn't give me enough time to send a PR :P

Thank you very much.
Coordinator
Mar 3, 2014 at 3:51 PM
I was pulling in an unrelated PR, so I figured I'd just include that directly to save a cycle :)