2013-02-15 32 views
3

我想在我的MVC 4應用程序中實現任務操作方法。一切都在後面工作,但它不是重定向。MVC4 .NET 4.5異步操作方法不重定向

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 
    } 
} 
+0

如果刪除了所有的'ClientHelper'代碼,並只留下'重定向(會發生什麼)'? – svick 2013-02-15 18:32:21

回答

4

我做了一個動作之後,我能夠使它工作,我也將它指向異步動作。我猜測如果你有任何異步操作方法重定向到另一個,那麼重定向也必須是異步的。

這裏只是一個簡單的例子

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(); 
} 
+0

這沒有多大意義。 – svick 2013-02-16 13:11:24

+1

晚會有點遲,但我想我已經清理了。他所做的一點是,如果調用者是異步的,那麼被調用的任務必須是異步的。 – statue 2013-12-27 16:07:06

+0

您應該在回答中顯示RedirectToAction方法的實現。 – 2017-02-26 06:54:36

0
[Authorize] 
    public class AccountController : Controller 
    { 
    [AllowAnonymous] 
     public ActionResult Login(string returnUrl) 
     { 
      ViewBag.ReturnUrl = returnUrl; 
      return View(); 
     } 

     // 
     // POST: /Account/Login 
     [HttpPost] 
     [AllowAnonymous] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) 
     { 
      if (ModelState.IsValid) 
      { 

       // find user by username first 
       var user = await UserManager.FindByNameAsync(model.Email); 

       if (user != null) 
       { 
        var validCredentials = await UserManager.FindAsync(model.Email, model.Password); 

        // When a user is lockedout, this check is done to ensure that even if the credentials are valid 
        // the user can not login until the lockout duration has passed 
        if (await UserManager.IsLockedOutAsync(user.Id)) 
        { 
         ModelState.AddModelError("", string.Format("Invalid credentials. Please try again, or contact support", 60)); 
        } 
        // if user is subject to lockouts and the credentials are invalid 
        // record the failure and check if user is lockedout and display message, otherwise, 
        // display the number of attempts remaining before lockout 
        else if (await UserManager.GetLockoutEnabledAsync(user.Id) && validCredentials == null) 
        { 
         // Record the failure which also may cause the user to be locked out 
         await UserManager.AccessFailedAsync(user.Id); 

         string message; 

         if (await UserManager.IsLockedOutAsync(user.Id)) 
         { 
          message = string.Format("Invalid credentials. Please try again, or contact support", 60); 
         } 
         else 
         { 
          int accessFailedCount = await UserManager.GetAccessFailedCountAsync(user.Id); 

          int attemptsLeft = (5 - accessFailedCount); 

          message = string.Format("Invalid credentials. Please try again, or contact support.", attemptsLeft); 
         } 

         ModelState.AddModelError("", message); 
        } 
        else if (validCredentials == null) 
        { 
         ModelState.AddModelError("", "Invalid credentials. Please try again, or contact support."); 
        } 
        else 
        { 
         await SignInAsync(user, model.RememberMe); 

         // When token is verified correctly, clear the access failed count used for lockout 
         await UserManager.ResetAccessFailedCountAsync(user.Id); 

         return RedirectToLocal(returnUrl); 
        } 
       } 
       else 
       { 
        ModelState.AddModelError("", string.Format("Invalid credentials. Please try again, or contact support", 60)); 
       } 
      } 
      // If we got this far, something failed, redisplay form 
      return View(model); 
     } 

    [HttpPost] 
     [AllowAnonymous] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model) 
     { 
      if (ModelState.IsValid) 
      { 
       var user = await UserManager.FindByNameAsync(model.Email); 
       if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id))) 
       { 
        // Don't reveal that the user does not exist or is not confirmed 
        //ModelState.AddModelError("", "The user either does not exist or is not confirmed."); 
        return RedirectToAction("ForgotPasswordConfirmation", "Account"); 
       } 
       else 
       { 
        var code = await UserManager.GeneratePasswordResetTokenAsync(user.Id); 
        var callbackUrl = Url.Action("ResetPassword", "Account", 
        new { UserId = user.Id, code = code }, protocol: Request.Url.Scheme); 

        string Data = System.IO.File.ReadAllText(Server.MapPath(@"~/documents/email_password_reset.txt")); 

        AspNetUser oUser = dbPortal.AspNetUsers.Find(user.Id); 
        // can't use string.format becuase of CSS     
        Data = Data.Replace("{0}", oUser.Name); // user name 
        Data = Data.Replace("{1}", callbackUrl); // URL to click 
        Data = Data.Replace("{2}", DateTime.Now.Year.ToString()); // copyright year 
        await UserManager.SendEmailAsync(user.Id, "Reset Password", Data); 
        return RedirectToAction("ForgotPasswordConfirmation", "Account"); 
       } 
      } 

      // If we got this far, something failed, redisplay form 
      return View(model); 

     } 

     // 
     // GET: /Account/ForgotPasswordConfirmation 
     [AllowAnonymous] 
     public async Task<ActionResult> ForgotPasswordConfirmation() 
     { 
      return View(); 
     } 
} 

上述解決方案並沒有爲我工作