【问题标题】:RedirectToAction is hit but not redirectingRedirectToAction 被命中但不重定向
【发布时间】:2019-03-06 12:52:51
【问题描述】:

我的操作重定向命中了聊天室/索引控制器中的方法,但实际上并没有重定向用户,它只是保持在同一页面上。我该如何解决?我可以通过 url 导航到视图,因此没有错误。

聊天室控制器

    [Authorize]
    public ActionResult ChatRoom()
    {
        return View();
    }

家庭控制器

  [AllowAnonymous]
  public ActionResult Index()
        {
            return View();
        }

    [HttpPost]
    [AllowAnonymous]
    public ActionResult Login(String username, String password)
    {
        bool ValidUser = UserDBController.ValidateUser(new Models.UserModel
        {
            UserName = username,
            PasswordHash = HashPass(password)
        });

        password = String.Empty;

        if (ValidUser)
        {
            FormsAuthentication.SetAuthCookie(username, true);
            return RedirectToAction("ChatRoom", "ChatRoom");
        }
        ViewBag.Username = username;
        return RedirectToAction("Index","Home");
    }

索引视图

    @{
    ViewBag.Title = "Home Page";
}

<div class="container">
    @Html.Partial("_LoginPartial")
</div>

部分登录

@{
    ViewBag.Title = "Login";
}


<div class="LoginForm">
    <div class="row">
        <div class="col-md-12 input">
            @Html.TextBox("Username", null, new { @class = "form-control", @id = "username", @placeholder = "Username", required = "required", value = !String.IsNullOrEmpty(ViewBag.username) ? ViewBag.username : "" })
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            @Html.TextBox("Password", null, new { @class = "form-control", @type = "password",  @id = "password", @placeholder = "Password", required = "required" })
        </div>
    </div>
    <button type="button" class="login-btn btn btn-primary">Login</button>
</div>

jQuery

$('.login-btn').click(function (e) {
    $.ajax({
        type: "POST",
        url: "/Home/Login",
        data: {
            userName: $('#username').val(),
            password: $('#password').val()
        }
    }).done(function (data) {
    });
});

【问题讨论】:

  • 你有自定义的 Authorize 类吗?喜欢UserAuthorize : System.Web.Mvc.AuthorizeAttribute
  • 如果你调试,是否命中Index()方法?
  • ahmeticat,不,我不知道。 @SeM 是的,Index 不正确时被击中,而 Chatroom() 正确时被击中。
  • 您对LoginIndex 的看法不同吗?
  • @SeM。我有一个包含我的登录部分视图的索引视图。我会更新主帖。

标签: c# asp.net-mvc


【解决方案1】:

那是因为你在做 ajax post。您可以返回 url 作为结果并使用该 url 进行重定向。

【讨论】:

  • 谢谢。实现了这个。
【解决方案2】:

更改您的登录功能

[AcceptVerbs(HttpVerbs.Post)]
public JsonResult Login(String username, String password)
{
    ...

    if (ValidUser)
    {
        FormsAuthentication.SetAuthCookie(username, true);
        return Json(false, JsonRequestBehavior.AllowGet);
    }
    return Json(true, JsonRequestBehavior.AllowGet);
}

和你的 AJAX 方法

$('.login-btn').click(function (e) {
        $.ajax({
            type: "POST",
            url: "/Home/Login",
            data: {
                userName: $('#username').val(),
                password: $('#password').val()
            },
            succcess:function (myJSONdata) {
                if(myJSONdata == true)
                    location.href = '/ChatRoom/ChatRoom';
                else
                    location.href = '/Home/Index';
            }
        }).done(function (data) {
        });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-09
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-15
    • 1970-01-01
    • 2017-08-05
    相关资源
    最近更新 更多