You need returnUrl to be published with the form message.
Perhaps the purest solution is to add returnUrl as a property in the LogOnViewModel:
public class LogOnViewModel { public string UserName { get; set; } public string Password { get; set; } public string ReturnUrl { get; set; } }
Your get method will set this value:
[HttpGet] public ActionResult LogOn(string returnUrl) {
In your form, you would save this value as a hidden field:
@using (Html.BeginForm()) { @Html.HiddenFor(model => model.ReturnUrl) // rest of form code left out for demo purposes }
Then your post method will have access to this value:
[HttpPost] public ActionResult LogOn(LogOnViewModel model) { if (ModelState.IsValid) { if (....) { string returnUrl = model.ReturnUrl; //.............. if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) return Redirect(returnUrl); return RedirectToAction(string.Empty, "home"); } else { //.............. } } return View(model); }
source share