asp.net mvc中的ReturnUrl因某些原因为空

4
public ActionResult LogOn(string returnUrl)
        {
            if (Request.IsAuthenticated)
            {
                return RedirectToAction(string.Empty, "home");
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(returnUrl))
                {
               //http://localhost:666/en-us/account/logon?returnurl=%2fen-us%2fadminka
                     //..............
                }
                return View();

            }
        }

        [HttpPost]
        public ActionResult LogOn(LogOnViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (....)
                {
                    //..............
                    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);
        }

HttpPost LogOn中,returnUrl始终等于null,即使在HttpGet LogOn中它不为null。为什么?我该如何解决?
3个回答

11
您需要将returnUrl与表单一起提交。可能最简洁的解决方案是将returnUrl作为LogOnViewModel的属性添加:
    public class LogOnViewModel
{
    public string UserName { get; set; }
    public string Password { get; set; }
    public string ReturnUrl { get; set; }
}

你的 get 方法将会设置那个值:

[HttpGet]
public ActionResult LogOn(string returnUrl)
    {
       // code for already authenticated case left out for demo
            var model = new LogOnViewModel { ReturnUrl = returnUrl };
            return View(model);

        }
    }
在您的表单中,您将该值作为隐藏字段进行持久化:
@using (Html.BeginForm())
{
    @Html.HiddenFor(model => model.ReturnUrl)

    // rest of form code left out for demo purposes
}

你的POST方法将能够访问该值:

    [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);
    }

2
在您的登录视图中,将ReturnUrl参数添加到表单操作中。例如:
BeginForm("LogOn", "Account", new {ReturnUrl = Request.QueryString["ReturnUrl"]})

请查看 davewasthere的回答

0

当在登录视图中时,请验证:

  • 要么URL包含“returnUrl”
  • 要么表单中有一个“returnUrl”字段

<<either the url contains "returnUrl">> 对不起,您的意思是什么?在第一个操作 LogOn(string returnUrl) 和 URL 中都有 ?returnurl= 参数。 - Alexandre
该表单没有 returnUrl 字段。 - Alexandre
你应该在你的表单中添加一个 returnUrl 字段。 - Softlion

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接