MVC页面到页面重定向

时间:2014-05-28 13:14:23

标签: asp.net-mvc-4 redirect

我是MVC的新手(使用4,框架4.0)并且我理解基础知识,但是这个页面重定向并没有像我期望的那样工作。该应用程序是登录/身份验证,如果用户成功登录,则将其重定向到目标应用程序。那部分工作正常。然而,用户可能忘记了他/她的登录凭证,因此我有一系列页面将提示用户注册的电子邮件地址和解码的验证码值。如果该信息被验证,则另一页面提示一系列(最多3个)预先确定的安全问题答案(在忘记密码的情况下)。如果成功应答安全挑战问题,则将用户重定向到密码更改页面。在该过程中的任何时刻,用户可以单击取消按钮,该按钮应该重定向回登录页面并清除跟踪其在恢复过程中的进度的任何状态变量。问题是我一直卡在甚至在RedirectToAction(“SomeAction”,“SomeController”)之后的页面上;我还留在页面上? URI甚至在浏览器上发生变化,但要求电子邮件地址或安全问题的页面保持活动状态。我正在使用ajax $ .get()来调用提交和取消的各种操作。

视图的定义如下:

@using (Html.BeginForm("RecoverUserCredentialsByModel", "Account", FormMethod.Get, new { id = "form1" }))
{
<!--... three input controls and a submit and cancel button-->
<p>
        <button id="btnSubmit" onclick="return CheckUserEmail()">Submit</button>
        <button id="btnCancel" onclick="return CancelRecovery()">Cancel</button>
</p>
}

<script type="text/javascript">
        function CheckUserEmail() {
            var emailAddress = document.getElementById("EmailAddress").value;
            var pictogramHref = document.getElementById("pictogramHref").src;
            var pictogramAnswer = document.getElementById("Pictogram").value;
            if (emailAddress != null) {
                var url = "/Account/ValidateEmail"; 
                $.get(url, { "emailAddress": emailAddress, "pictogramHref": pictogramHref, "pictogramTranslation": pictogramAnswer }, null);
                return true;
            }
        }
</script>

<script type="text/javascript">
    function CancelRecovery() {
        var url = "/AuthenticationModule/Account/CancelRecovery";
        $.get(url, {}, null);
        return true;
    }
</script>

Codebehind重定向看起来像:

/// <summary>
/// Global cancel recovery, clears the stateful session object and redirects back to login view
/// </summary>
/// <returns>ActionResult</returns>
[AllowAnonymous]
public ActionResult CancelRecovery()
{
        LoginModel statefulLoginModel = null;
        try
        {
            // Reset everything active and redirect to login view
            statefulLoginModel = new LoginModel();
            Session["LoginModel"] = statefulLoginModel;
            return Redirector(statefulLoginModel);
        }
        catch (Exception ex)
        {
            // Log the error and Reset everything active and redirect to login view
            FileLogger.Log(ex);
            statefulLoginModel = new LoginModel();
            Session["LoginModel"] = statefulLoginModel;
            return Redirector(statefulLoginModel);
        }
}

[AllowAnonymous]
public ActionResult Redirector(LoginModel model)
{
... some code
            Session["LoginModel"] = statefulLoginModel;
            if (loginState == 0)
            {
                RedirectToAction("LogOn");
            }

}

当它命中RedirectToAction(“LogOn”)时;视图“RecoverUserInfo”在浏览器上保持活动状态,不会发生重定向?

我做错了什么?

1 个答案:

答案 0 :(得分:1)

试试这个..........

正确的语法是return RedirectToAction("ActionName","ControllerName");

在这种情况下,如果Logon Action写在同一个Controller上,那么请使用以下代码..

return RedirectToAction("LogOn");

或者它写在另一个控制器上,然后只需在以下代码中替换您的操作名称和控制器名称。

return RedirectToAction("ActionName","ControllerName");