剃刀重定向包含意外行为

时间:2017-09-28 15:14:48

标签: asp.net-mvc razor

我有一个非常简单的登录页面。问题出在Login方法中。我执行RedirectToAction,但它永远不会将网页从登录页面更改为主页。

HomeController(主应用程序控制器)

    public ActionResult Index(string username = null, string password = null)
    {
        if (username == null || password == null)
        {
            return RedirectToAction("Index", "Auth");
        }

        this.username = username;
        this.password = password;

        return View(); //and have tried return View("Index");
    }

AuthController(抓取用户名和密码)

    public ActionResult Login(string username, string password)
    {           
        return RedirectToAction("Index","Home", new { username=username,password=password});
    }

按钮按

function Login() {
    var _get_searchdetails = '@Url.Action("Login", "Auth")';
    $.ajax({
    url: _get_searchdetails,
    data: { username: $('#username').val(), password: $('#password').val() },
    type: "POST",
    beforeSend: function () {

    },
    error: function (xhr, textStatus, error) {
        alert("Try again!");
    },
    success: function (htmldata, xhr, settings) {

    }
    });
}

2 个答案:

答案 0 :(得分:0)

实际上不清楚你在这里要做什么,因为你的动作方法代码没有意义。

但是这里更好的选择可以是使用RedirecToAction method来指定控制器和动作名称及其参数,如:

return RedirectToAction("Index","Home", new { username=username,password=password});

并且您不应该使用Get请求将用户名和密码传递给操作方法,因为它很容易被黑客轻易入侵Web应用程序。

希望它有帮助!

答案 1 :(得分:0)

找到我的问题。我需要这样做而不是ajax查询。

        @using (Html.BeginForm("Login", "Auth"))
        {
            <label for="username">Username</label>
            <input type="text" id="username" name="username"/>
            <br />
            <label for="username">Password</label>
            <input type="password" id="password" name="password"/>

            <input type="submit" value="Create" />
        }