【发布时间】:2020-04-30 19:37:29
【问题描述】:
我有一个使用 Ajax 表单的登录页面。在调用的动作中,我想根据某些标准重定向到不同的控制器/动作。目前,我并不担心这一点,只是试图让重定向工作。
ActionFilterAttribute 好像很复杂:
public class AjaxAwareRedirectAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
var result = filterContext.Result as RedirectResult;
if (result != null && filterContext.HttpContext.Request.IsAjaxRequest())
{
string destinationUrl = UrlHelper.GenerateContentUrl(result.Url, filterContext.HttpContext);
filterContext.Result = new JavaScriptResult()
{
Script = string.Format("window.location = '{0}';", destinationUrl)
};
}
}
}
在登录页面控制器操作调用后触发
return RedirectToAction("Index", "TwoFactorAuthentication");
重定向被忽略并重新显示登录页面。
整个站点的许多地方都使用了 AjaxAwareRedirectAttribute 属性,所以我需要小心我对它做什么(如果有的话)。
如果我使用简单的
return Redirect("/en/authentication");
那么这似乎工作正常。但是,我也想像这样传递模型数据
return RedirectToAction("Index", "TwoFactorAuthentication", member);
我现在不确定我是否接近并走在正确的轨道上,还是完全错了!
这是一个 Umbraco 7 项目,如果这与它有任何关系的话。
编辑:
我是否需要在客户端检测/处理重定向?
这是 Ajax 表单
@using (Ajax.BeginForm("Login", "LoginSignup", null, options, null))
这些是选项
var options = new AjaxOptions
{
HttpMethod = "POST",
OnFailure = "ShowError",
OnSuccess = "ShowSuccess",
UpdateTargetId = "target1",
InsertionMode = InsertionMode.Replace
};
以及相关的脚本
<script>
function ShowError(response) {
$("#feedback").show();
$("#signupfeedback").show();
$("#status").html("@Umbraco.Field("#Login.ScriptErrorPostingForm")");
}
function ShowSuccess(response, textStatus, xhr) {
if (xhr.responseText.indexOf('window') == -1) {
$("#feedback").show();
$("#signupfeedback").show();
$(".validation-summary-errors").hide();
}
}
function ShowExternalLoginError() {
$("#status").removeClass();
$("#status").addClass("b_noteList");
$("#status").html("@Umbraco.Field("#Login.ScriptErrorOccurred")");
}
</script>
【问题讨论】:
-
你是如何处理客户端的? Ajax 和重定向可能很棘手。
-
@KevinBBurns - 我已经用相关的 Ajax 详细信息更新了我的问题。
标签: c# asp.net-mvc controller umbraco7