Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 重定向到登录后返回url_Asp.net Mvc_Asp.net Mvc 3 - Fatal编程技术网

Asp.net mvc 重定向到登录后返回url

Asp.net mvc 重定向到登录后返回url,asp.net-mvc,asp.net-mvc-3,Asp.net Mvc,Asp.net Mvc 3,我的razor视图中有这样一个链接: <a href="Home/Login?ReturnUrl=Disputes/Index"> disputes</a> 鉴于我使用的是: public ActionResult Login(string returnUrl) { if (string.IsNullOrEmpty(returnUrl) && Request.UrlReferrer != null) returnUrl = S

我的razor视图中有这样一个链接:

 <a href="Home/Login?ReturnUrl=Disputes/Index"> disputes</a>
鉴于我使用的是:

 public ActionResult Login(string returnUrl) {
   if (string.IsNullOrEmpty(returnUrl) && Request.UrlReferrer != null)
         returnUrl = Server.UrlEncode(Request.UrlReferrer.PathAndQuery);

   if (Url.IsLocalUrl(returnUrl) && !string.IsNullOrEmpty(returnUrl))
   {
      ViewBag.ReturnURL = returnUrl;
   }

   return View();
 }
 @Html.Hidden("returnUrl",@Request.QueryString)
然后在后处理方法中:

 public ActionResult LogOn(LogOnModel model, string returnUrl)
 {
   if (ModelState.IsValid)
   {
      if (membershipService.ValidateUser(model.UserName, model.Password, model.Type))
      {
         formsAuthenticationService.SignIn(model.UserName, model.RememberMe);
         SetUserInfo(model.UserName);

         string decodedUrl = "";
         if (!string.IsNullOrEmpty(returnUrl))
            decodedUrl = Server.UrlDecode(returnUrl);

         if (Url.IsLocalUrl(decodedUrl))                    
            return Redirect(decodedUrl);
         else
            return Redirect("Home", Index);

      }
   }
 }
它正在重定向到:
/disferences/Index
,但它应该转到
myApp/disferences/Index
,其中带有查询字符串的url如下所示<代码>/myApp/Home/Login?返回URL=/Disputes/Index


如何解决此问题?

在这种情况下,您可以让登录操作使用returnUrl参数,如果参数不为空,您可以返回
重定向(returnUrl),而不是重定向到主页/索引。在创建新项目时,请查看VS生成的默认AccountController。它正是这样做的。

我结合使用上述建议和
Request.urlreferer
来获取以前的位置:

    public ActionResult LogOn(string returnUrl)
    {
        //So that the user can be referred back to where they were when they click logon
        if (string.IsNullOrEmpty(returnUrl) && Request.UrlReferrer != null)
            returnUrl = Server.UrlEncode(Request.UrlReferrer.PathAndQuery);

        if (Url.IsLocalUrl(returnUrl) && !string.IsNullOrEmpty(returnUrl))
        {
            ViewBag.ReturnURL = returnUrl;
        }
        return View();
    }
这样,我就不必将位置放在
ActionLink

我使用
ViewBag.ReturnURL
在登录页面中填充一个隐藏字段。然后在登录HTTPPost
ActionResult
中,我将用户重定向到隐藏字段中的位置(如果有):


如果
ReturnURL
null
,请确保按如下方式从视图调用操作方法:

// FormMethod.post is optional
@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post))
{
    // login view html
}
客户控制员:

[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
    ViewBag.ReturnUrl = returnUrl;
    return View();
}

谢谢你的快速回复。我写了这篇文章,但在action方法中returnUrl仍然是空的。我是否在href中创建了错误的返回url?请建议您可能需要对值进行url编码:
。不,编码后相同:登录操作方法中的returnurl为null,其中查询字符串为returnurl,如下所示:公共操作结果登录(LogOnModel,字符串returnurl)and/Home/Login?ReturnUrl=disferences%2fIndex在登录页面中使用了隐藏字段,现在我可以在登录操作方法中看到ReturnUrl值,如下所示:ReturnUrl=%2finders%2fIndex但在这里失败:if(Url.islocalur(ReturnUrl)和&ReturnUrl.Length>1&&ReturnUrl.StartsWith(“/”&!ReturnUrl.StartsWith(“/”)&&&!returnUrl.StartsWith(“/\\”)返回重定向(returnUrl);在我的登录后操作中,我有:string decodedUrl=“”;如果(!string.IsNullOrEmpty(returnUrl))decodedUrl=Server.UrlDecode(returnUrl);如果(Url.islocalur(decodedull)){return Redirect(decodedull);}这对我来说很有用。URL确实有效吗?它正在重定向到此:/Distives/Index,但它应该转到myApp/Distives/Index,因为带有querystring的URL如下所示/myApp/Home/Login?ReturnUrl=/Disferences/Index当框架自动完成时,为什么要将值存储在ViewBag中?尝试使用(带“/”)或甚至不使用查询字符串来测试URLreferer方法。@Oliver:我尝试了不带查询字符串的方法,但它不起作用……是的,正确处理这个问题很重要。对我来说,在HTML中设置类的匿名对象被用作路由值的anon对象,因此ReturnUrl不会返回,而是在查询参数中看到“class”。最好检查最终呈现的标记。我使用这个:
@使用(Html.BeginForm(“BetaLogin”,“Authentication”,new{ReturnUrl=this.Request.Params[“ReturnUrl”]},FormMethod.Post,new{@class=“o-box-section”}))
在表单上设置返回url对我来说非常有效,代码更改最少。谢谢试着解释你的答案,因为它对其他人有用。
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
    ViewBag.ReturnUrl = returnUrl;
    return View();
}
        //Utilities         
        public static ReturnUrl GetReturnUrlValues(string returnUrl)
        {
            //0:Action,1:Controller,2:Area:,3:Id
            ReturnUrl vm = new ReturnUrl();
            using (TBBSEntities context = new TBBSEntities())
            {
                if (returnUrl != null && returnUrl.Length > 10)
                {
                    var counterValue = returnUrl.Split('/');
                    vm.Action = counterValue[0];
                    vm.Controller = counterValue[1];
                    vm.Area = counterValue[2] == "0" ? "" : counterValue[2] ;
                    vm.Id = Convert.ToInt32(counterValue[3]);
                    vm.NullStatus = true;
                    return vm;
                }
                vm.NullStatus = false;
                return vm;
            }
        }

        //Controller
        var transformUrl = Utilities.GetReturnUrlValues(returnUrl);
            if (transformUrl.NullStatus)
            {
            return RedirectToAction(transformUrl.Action, transformUrl.Controller,
                    new { area = transformUrl.Area, id = transformUrl.Id });
            }
            return RedirectToAction("Action", "Controller", new { area = "Area", Id});


        //View
        var returnUrl = "Action/Controller/Area/" + @Model.Id;