Redirect to Action is not working With ASP.NET core

浪子不回头ぞ 提交于 2019-12-11 01:14:46

问题


I have my login view which wants to redirect to Clients page after successful login.

To do that I am using ,

[HttpPost]
        public async Task<IActionResult> Login(LoginViewModel model)
        {
            if (ModelState.IsValid)
            {
                var result = await _signInManager.PasswordSignInAsync(model.Username, model.Password, model.Rememberme, false);

                if (result.Succeeded)
                {

                    if (Request.Query.Keys.Contains("ReturnUrl"))
                    {
                        Redirect(Request.Query["ReturnUrl"].First());
                    }
                    RedirectToAction("Index", "Clients");
                }
                else
                {
                    ModelState.AddModelError("", "Failed to login");
                    return View();
                }
            }

            return View();

        }

But after successful login , I am staying in current login page and not redirected to Client Page!! If I try to navigate to Client page using browser this works fine without any issue.


回答1:


You need to return method result, just add return to all redirect methods:

return RedirectToAction("Index", "Clients");

Redirect methods are returnig view, to show this view You need to return it.

More info about IActionResult



来源:https://stackoverflow.com/questions/48221704/redirect-to-action-is-not-working-with-asp-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!