In the usual AccountController in my MVC3 app, if returnUrl is set (in my case, I set it manually), it will call Redirect(returnUrl).
Assume my return URL is /Admin/HealthCheck (which it really is). When I'm debugging, I get a URL like http://localhost:3279/Admin/HealthCheck from the redirect call.
Then, I deployed my app to http://localhost/Test. In this case, Redirect(returnUrl) redirects me to http://localhost/Admin/HealthCheck and not the expected http://localhost/Test/Admin/HealthCheck.
What's going on here? How do I fix this (if it is fixable)?
Below is a snippet from the (standard) MVC3 AccountController; you can see where I get the return URL from the query string (eg. http://localhost/Test/LogOn?ReturnUrl=/Admin/HealthCheck, albeit URL encoded).
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.UserName, model.Password))
{
returnUrl = Request.Params["ReturnUrl"];
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
You haven't actually shown how this manual setting happens, but if you have hardcoded an url such as
/Admin/HealthCheckinstead of using an url helper to generate this url such asUrl.Action("HealthCheck", "Admin")don't expect miracles to happen.Your
LogOnis fine. It does what it is supposed to do => it redirects to the url that is passed as argument. Your problem lies in the way you are setting this url.Conclusion: in an ASP.NET MVC application always use url helpers when dealing with urls. Never hardcode them.