【发布时间】: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() 正确时被击中。
-
您对
Login和Index的看法不同吗? -
@SeM。我有一个包含我的登录部分视图的索引视图。我会更新主帖。
标签: c# asp.net-mvc