【发布时间】:2017-09-19 21:28:50
【问题描述】:
我有一个简单的登录屏幕。单击按钮时,我想调用 Home 控制器中定义的 LoginUser 操作。我收到 HTTP 404,找不到资源错误。点击按钮后的地址栏显示“http://localhost:13805/Home/LoginUser”。我不确定我在这里做错了什么。非常感谢您对此的任何帮助。
这是按钮:
<button type="submit" onclick="location.href='@Url.Action("LoginUser", "Home")'" class="btn btn-default">Submit</button>
这是 Home 控制器中定义的操作:
[HttpPost]
public ActionResult LoginUser(string username, string password)
{
bool loginSuccess = false;
if (!HttpContext.User.Identity.IsAuthenticated)
{
AuthenticationMode = System.Configuration.ConfigurationManager.AppSettings["AuthenticationMode"];
if (AuthenticationMode.Equals("Forms"))
{
int userId = CheckUserLogin(username);
if (userId > 0)
{
CurrentUserId = userId;
FormsAuthentication.SetAuthCookie(username, false);
LoadMenu();
loginSuccess = true;
}
else
{
loginSuccess = false;
}
}
else
{
loginSuccess = false;
}
}
else
{
loginSuccess = true;
}
if (loginSuccess)
{
return RedirectToAction("Index", "home");
}
else
{
return RedirectToAction("Login", "home");
}
}
【问题讨论】:
-
为什么你有一个
onclick()事件调用一个GET方法,附加到一个提交按钮调用一个POST方法?你想做什么?
标签: asp.net-mvc