Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 278
Jatin Avatar asked Sep 13 '11 13:09

Jatin


People also ask

Why is 301 not working?

The reasons for 301 redirect not working are much more well-defined among WordPress sites. One of the main causes is because you have added the rewrite rules on both the cPanel “Redirects” tool and from your WordPress plugin.

How do I redirect a URL to another URL?

Click the URL Redirects tab. In the upper right, click Add URL redirect. In the right panel, select the Standard or Flexible redirect type. A standard redirect is used to redirect one URL to another.

How do I enable URL redirection in Chrome?

To allow the Pop-ups and Redirects in Chrome Computer, tap on the three dots at the top right of the corner and open the settings and then tap on the Privacy and Security section. Now, tap on the Pop-up and redirects and select the checkbox next to the option stating Sites can send pop-ups and use redirects.


2 Answers

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;
}
like image 163
Darin Dimitrov Avatar answered Sep 30 '22 19:09

Darin Dimitrov


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

like image 40
Ravi Ganesan Avatar answered Sep 30 '22 18:09

Ravi Ganesan