1
votes

I have an Asp.net MVC program, it cannot redirect to root "/" after login successfully. It works pervious. Login URL: http://localhost:8080/Account/Login?ReturnUrl=%2f

After Login, it should be http://localhost:8080/ .
But it shows http://localhost:8080/Account/Login?ReturnUrl=/
If it shows this URL, the layout page will keep using LoginLayout. If I type in the root URL - http://localhost:8080/ manually, it shows correctly.

Here is my Controller code:

[HttpGet]
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
    ViewBag.ReturnUrl = returnUrl;
    LoginViewModel model = new LoginViewModel();
    return View(model);
}

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            var user = await UserManager.FindAsync(model.UserName, model.Password);
            if (user != null)
            {
                await SignInAsync(user, model.RememberMe);

                return RedirectToLocal(returnUrl);
            }
            else
            {
                ModelState.AddModelError("Error", "Invalid username or password.");
            }
        }

        return View(model);
    }

View Code:

@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post))

Thanks Wilson

1
What is the value of returnUrl in the second ([HttpPost]) action when the problem occurs? It could be that it's empty or null, then Redirect would actually redirect to the same page (which is indeed /Account/Login). - haim770
Are you sure the user is logged in ? This is the way the controller should behave if var user = await UserManager.FindAsync(model.UserName, model.Password); will return null. - Ondrej Svejdar
Hi haim770, what is the mean in the second HttpPost action. I set the breakpoint in login post action and it redirect to "/". In this case, it should be Home/Index and does go to Home controller and Index action. - Wilson Wu
Hi Ondrej Svejdar, login should be done and it goes to Home controller and Index action. - Wilson Wu

1 Answers

0
votes

The problem is solved by deleting and adding all js and css files again. The problem is file versioning.

Thank you again.