Jatin
Jatin

Reputation: 4063

Redirect(url) not working

In context of ASP.net MVC3, I have this line of code in a controller action, which tries to redirect to a particular url.

return Redirect(returnUrl);

returnUrl is a string that contains "/Home/Index/". For Some reason the redirect is not taking place and I remain on the same screen. I tried removing the trailing slash but no success. Any ideas why the Redirect does not take place?

Upvotes: 12

Views: 18079

Answers (4)

laryhil
laryhil

Reputation: 1

@Nirvan, i had the same problem. after thorough research i realized that the default login settings that come with the mvc template were conflicting with my login settings. i never found out how to solve it and decided to restart the project i was working on with a blank mvc project.(i didnt want to use the default login setings AND CODE done by mycrosoft). it worked smoothly. @Nirvan,if your using mvc template your code is most likely fine

Upvotes: 0

Julio Luzardo
Julio Luzardo

Reputation: 151

I had this same problem in MVC 5, the standard login.cshtml markup was working fine. I was only having problems when I was including the html markup for a template I had bought. So what I did was to start replacing the login.cshtml code bit by bit. As it turns out the problem was because the template included a JQuery plugin called "rd-mailform" and this was causing the issue.

This didn't work:

@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "rd-mailform form-modern form-darker", role = "form" }))

And this worked:

@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-modern form-darker", role = "form" }))

Sometimes designers that create these templates mix functionality with design, so you have to watch out for that.

Upvotes: 0

Ravi Ganesan
Ravi Ganesan

Reputation: 295

If you are stuck on the SAME LOGIN SCREEN after supplying valid logon credentials, it is possible you have not set the Forms authentication cookie.

Whenever you use Redirect or RedirectToLocal in your Login actionmethods, make sure you call in the following order:

FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
return RedirectToLocal(returnUrl);

This ensures, the cookie is set before Redirecting, otherwise the client will treat as if the user is not logged in.

Thanks

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1039498

The Redirect method is intended to be used to redirect to external urls of your site and by passing it an absolute url. If you need to redirect to another controller action that belongs to your site it would be better to use this:

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

This way you are no longer hardcoding urls and your code is less fragile to route changes.

This being said, if you are invoking the controller action that performs this redirect with AJAX you cannot expect it to redirect the browser anywhere => it will obviously stay on the same page. The AJAX request will succeed following all redirects and in the success callback you will get the final HTML of the /Home/Index url as if it was requested without AJAX.

If you want to redirect in the success callback of an AJAX call you could have your controller action return for example a JSON object indicating the target url you want to redirect to:

return Json(new { redirectToUrl = Url.Action("Index", "Home") });

and in your callback use the window.location.href function:

success: function(result) {
    window.location.href = result.redirectToUrl;
}

Upvotes: 21

Related Questions