我正在将字符串传递给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);
}
这是Index方法,也在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 _Login RedirectToLocal之后,再次调用主索引方法。不确定是谁/什么叫它。可能是我想念的简单事。
为了澄清我在这里发布了更多的View数据:
/提交/索引:
@{
Layout = "~/Views/Shared/_Home.cshtml";
}
<div>
<p>...</p>
</div>
/共享/ _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>
/共享/ _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>
登录逻辑有效,用户可以登录。只是这个重定向搞乱了。
答案 0 :(得分:0)
您正在调用_Login
部分,但部分没有关联的操作。它可能是一个儿童行动,但您不能发布到儿童行动。因此,_Login
只是一个标准操作,除非您通过以下内容直接发布:
@using (Html.BeginForm("_Login"))
{
...
}
然后在结果页面上提交该表单,操作永远不会被命中。
假设你实际上已经这样做了,并且只是对术语感到困惑,那么接下来要看的是返回URL变量的不一致性。您的操作接受returnUrl
作为参数,但如果用户未登录,您只能使用该操作,并使用该返回网址重定向到Index
。但是,如果用户已登录,则您使用RedirectToLocal
来呼叫model.ReturnUrl
。