C# – MVC4 .NET 4.5 async action method does not redirect

.net-4.5asp.net-mvcasync-awaitc

I am trying to implement a task action method in my MVC 4 application. Everything works on the back in, but it is not redirecting.

public class AccountController : AsyncController 
{
    [HttpPost]
    [AllowAnonymous]
    public async Task<ActionResult> Login(LoginModel model, string returnUrl)
    {
        var client = new ClientHelper("login");
        account = await client.CallActionType<LoginModel, Account>(EnumHelpers.HttpType.Post, model);

        if (account != null)
        {
            validLogin = true;
        }

        return Redirect(returnUrl); // This is called but the page does not redirect, just sits a load
    }
}

Best Answer

I was able to get it working after making the Action I was directing it to an async action as well. I am guessing if you have any async action method redirecting to another then that redirect must be async as well.

Here is just a quick example

public async Task<ActionResult> Login(LoginModel model) {
    //You would do some async work here like I was doing.

    return RedirectToAction("Action","Controller");//The action must be async as well
}
public async Task<ActionResult> Action() {//This must be an async task 
    return View();
}