我正在向Redirect传递一个字符串,但是控制器没有将浏览器发送到适当的位置。
字符串为:"/Admin/SystemSecurity/_PermissionDetail/1“
代码是:
public ActionResult RedirectToLocal(string returnUrl)
{
if (Url.IsLocalUrl(returnUrl))
{
// Code get's here, but seems to go to /Submission/Index
return Redirect(returnUrl);
}
return RedirectToAction("Index", "Submission");
}在本例中,调用RedirectToLocal的方法是SubmissionController中的_Login:
[ChildActionOnly]
public ActionResult _Login(string returnUrl)
{
if (Request.Cookies["UserName"] != null && !string.IsNullOrEmpty(Request.Cookies["UserName"].Value))
{
var loginModel = new Login { Email = Request.Cookies["UserName"].Value, ReturnUrl = returnUrl};
return PartialView(loginModel);
}
return PartialView();
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> _Login(Login model, string returnUrl)
{
var isLoggedIn = UserLogin(model.Email, model.Password);
if (!isLoggedIn)
{
TempData["ErrorMessage"] = "Invalid email address or password.";
return RedirectToAction("Index", new { returnUrl = returnUrl });
}
// I make the call here, the values is correct here.
return RedirectToLocal(returnUrl);
}下面是索引方法,也是用SubmissionController编写的:
public ActionResult Index(string message, string returnUrl)
{
IsAuthenticated();
if (!string.IsNullOrEmpty(message) )
AddMessage(message);
if (!string.IsNullOrEmpty((string)TempData["ErrorMessage"]))
{
AddError((string)TempData["ErrorMessage"]);
}
ViewBag.ReturnUrl = returnUrl;
return View();
}在POST索引RedirectToLocal之后,会再次调用主索引方法。不确定是谁/什么叫它。可能是我漏掉了一些简单的东西。
为了澄清,我在这里发布了更多的View数据:
/提交/索引:
@{
Layout = "~/Views/Shared/_Home.cshtml";
}
<div>
<p>...</p>
</div>/Shared/_Home
@using PublicationSystem.ViewModels
@{
ViewBag.Title = "_Home";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="container-fluid home-banner">
<!--Content from Index Page -------------------------------------->
<div class="clearfix"></div>
<div class="container">
<div class="row">
<div class="col-sm-4 col-md-3 col-lg-3">
<div class="left-side-blue">
...
</div>
</div>
<div class="col-sm-8 col-md-9 col-lg-9">
@{ Html.RenderPartial("_ErrorMessages"); }
@if (!ViewBag.IsAuthenticated)
{
Html.RenderAction("_Login", new { returnUrl = ViewBag.ReturnUrl });
}
else
{
<div class="hp-nav-boxes">...</div>
}
</div>
</div>
</div>
...
</div>/Shared/_Login:
@model PublicationSystem.ViewModels.Login
<div class="login-box">
<div class="row">
@using (Html.BeginForm("_Login", "Submission", new { returnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "col-sm-6 col-md-4 col-lg-4 pull-right custm-login ipadsm4" }))
{
@Html.AntiForgeryToken()
<div class="form-group">
@Html.TextBoxFor(m => m.Email, new { @class = "form-control", @placeholder = "Email" })
@Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })
</div>
<div class="form-group">
@Html.PasswordFor(m => m.Password, new { @class = "form-control", @placeholder = "Password" })
@Html.ValidationMessageFor(m => m.Password, "", new {@class = "text-danger"})
</div>
<button type="submit" class="btn btn-default pull-right">
Login <span aria-hidden="true" class="glyphicon glyphicon-play"></span>
</button>
<div class="clearfix"></div>
}
</div>登录逻辑起作用,用户可以登录。只是这个重定向搞砸了。
发布于 2015-09-04 00:51:11
您将_Login称为partial,但是partials没有关联的操作。它可能是子操作,但您不能发布到子操作。因此,_Login只是一个标准操作,除非您直接通过类似以下内容发布到它:
@using (Html.BeginForm("_Login"))
{
...
}然后在结果页面上提交该表单,则该操作将永远不会被选中。
假设您实际上已经这样做了,并且只是对术语感到困惑,那么下一个要查看的地方是返回URL变量中的不一致。您的操作接受returnUrl作为参数,但只有在用户未登录时才使用该参数,在那里您使用返回的URL重定向到Index。但是,如果用户已经登录,那么您将使用model.ReturnUrl调用RedirectToLocal。
https://stackoverflow.com/questions/32380772
复制相似问题