【问题标题】:returnUrl parameter is not passing to Post method of LogOn actionreturnUrl 参数未传递给 LogOn 操作的 Post 方法
【发布时间】:2012-07-25 08:37:57
【问题描述】:

当用户尝试访问经过身份验证的页面时,它会被重定向到登录页面。以下是登录操作:

控制器动作

 [HttpPost]
  public ActionResult LogOn(LogOnModel model, string returnUrl)
  {
    //Authentication codes goes here .......
  }

登录查看

  @using (Html.BeginForm("LogOn", "Account", FormMethod.Post, new {  @class = "jmenu" }))
   {
     // Form for login codes goes here......
  }

问题: 在 LogOn View 中,当我没有在 Html.BeginForm() 中输入参数时,它可以正常工作。该页面在登录后被重定向到用户被重定向到登录以进行身份​​验证的页面。但是如果我像上面一样传递参数,登录后它会被重定向到主页。在这里,可能是由于 returnUrl 没有传递给 LogOn 操作。如何在 Html.BeginForm 中为 LogOn 操作传递 returnUrl 参数。或者,是否可以不更改视图并在 LogOn 操作中进行修改,这样我就不必更改其他视图的代码。感谢您的宝贵时间...

【问题讨论】:

标签: asp.net-mvc-3 razor


【解决方案1】:

您可以在操作中使用Request.UrlReferrer 来获取最后一个请求的 URL。

【讨论】:

    【解决方案2】:

    您需要将返回网址定义为表单的一部分。需要注意的一件事是,您不能总是依赖 url 引荐来源网址。在某些情况下,它可能为 null。

    @{
        string url = "";
        if (ViewContext.HttpContext.Request.UrlReferrer != null)
        {
            url = ViewContext.HttpContext.Request.UrlReferrer.PathAndQuery;
        } 
    }
    
    @using (Html.BeginForm("Logon", "Account", new { model = this.Model, returnUrl = url }))
    {
    }
    
    猜你喜欢
    • 1970-01-01
    • 2011-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多