http://www.codeproject.com/Articles/843044/Getting-started-with-AngularJS-and-ASP-NET-MVC-The
我使用上面链接中的教程构建了一个ASP.NET + AngularJS应用程序。
$scope.login = function () {
console.log("DERP");
var result = LoginFactory($scope.loginForm.emailAddress, $scope.loginForm.password, $scope.loginForm.rememberMe);
result.then(function (result) {
if (result.success) {
if ($scope.loginForm.returnUrl !== undefined) {
console.log("im in");
$location.path('/home');
} else {
console.log("login failed");
$location.path($scope.loginForm.returnUrl);
}
} else {
$scope.loginForm.loginFailure = true;
}
});
}
执行此功能时,HTTP POST请求会运行一段时间,然后返回内部服务器错误。它不会成功或失败。
POST http://localhost:54865/Account/Login 500 (Internal Server Error)
(anonymous function) @ angular.js:8495
t @ angular.js:8291
g @ angular.js:8025
J @ angular.js:11498
J @ angular.js:11498
(anonymous function) @ angular.js:11584
k.$eval @ angular.js:12608
k.$digest @ angular.js:12420
k.$apply @ angular.js:12712
(anonymous function) @ angular.js:18980
(anonymous function) @ angular.js:2823
q @ angular.js:325c @ angular.js:2822
我是ASP.NET MVC的新手,所以我对这些概念没有很好的把握。但我怀疑问题出在我的RouteConfig.cs
文件中,教程指示我明确说明每个AngularJS视图的所有不同路由。我唯一的/Account/Login
路线如下:
routes.MapRoute(
name: "login",
url: "Account/Login",
defaults: new { controller = "Account", action = "Login" });
如果我要创建一个MVC路由来接受对帐户/登录的HTTP POST请求,我该如何实现?我完全偏离了轨道吗?
修改
以下是我的AccountController.cs
文件的相关部分:
[AllowAnonymous]
public ActionResult Login()
{
return View();
}
[HttpPost]
[AllowAnonymous]
public async Task<bool> Login(MyApp.Entities.ViewModels.LoginViewModel model)
{
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, false);
switch (result)
{
case SignInStatus.Success:
return true;
default:
ModelState.AddModelError("", "Invalid login attempt.");
return false;
}
}