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
returnUrlin the second ([HttpPost]) action when the problem occurs? It could be that it's empty or null, thenRedirectwould actually redirect to the same page (which is indeed/Account/Login). - haim770