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?
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.
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.
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.
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;
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With