【问题标题】:ReturnUrl Duplicating Parameters - MVC 4ReturnUrl 重复参数 - MVC 4
【发布时间】:2013-09-07 04:00:58
【问题描述】:

我遇到 retunUrl 问题,我的 QueryString 参数重复。

我的网址是这样的:

“www.mysite.com/Order/?id=1&item=123”

然后,将我重定向到登录页面,网址如下:

“www.mysite.com/login/RedirectUrl=/Order?id=1&item=123&id=1&item=123

用户登录后,动作重定向到:

“www.mysite.com/Order/?id=1&item=123&id=1&item=123”

在我的页面中,当我使用 Request.QueryString["id"] 时出现错误,因为查询字符串“ID”重复。

我的登录操作代码如下所示:

[HttpPost]
[AllowAnonymous]
public ActionResult Index(LoginModel model, string ReturnUrl)
{
    if(VerifyLogin(model))
    {
       if(ReturnUrl != null)
          return Redirect(ReturnUrl);//redirect to url with duplicated parameters
       else
          return Redirect("/Home");
    }
    else
    {
       ModelState.AddModelError("", "Invalid Username or Password");
    }

   return View();
}

我该如何解决这个问题?

【问题讨论】:

    标签: asp.net-mvc duplicates query-string


    【解决方案1】:

    我认为问题出在 Order 控制器或 ReturnUrl 分配逻辑上。它可能会添加 url+queryString。如果是,您可以尝试类似 Request.Url.GetLeftPart(UriPartial.Path) + queryString

    【讨论】:

    • 嗨!感谢您的回复。没用 =(... Request.Url.GetLeftPart(UriPartial.Path) 返回 "www.mysite.com" 和 Request.QueryString.ToString() 返回 "RedirectUrl=/Order?id=1&item=123&id=1&item=123"因为在那一刻查询字符串是“RedirectUrl”
    • 再次感谢,但这不是我的情况...我需要查询字符串,但我不需要重复的查询字符串。我知道如何获取第一个查询字符串,即使这是重复的,但我不想对我的所有页面都这样做......我有 20 + ......我需要在登录操作中解决这个问题。
    猜你喜欢
    • 2013-07-07
    • 2011-02-10
    • 2012-10-12
    • 2015-06-16
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多