我正在尝试在我的Web API中配置基于令牌的身份验证,我想为其创建一个表单以便为新用户提供用户。 以下是我的代码 -
Web API:
// POST api/Account/Register
[AllowAnonymous]
[Route("Register")]
public async Task<IHttpActionResult> Register(RegisterBindingModel model)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var user = new ApplicationUser();// { UserName = model.Email, Email = model.Email };
IdentityResult result = await UserManager.CreateAsync(user, model.Password);
if (!result.Succeeded)
{
return GetErrorResult(result);
}
return Ok();
}
Angular JS:
$scope.registerUser = function () {
var userData;
var serverBaseUrl = "http://localhost:61086";
var accountUrl = serverBaseUrl + "/api/Account/Register";
var userData = {
Email: $scope.email,
Password: $scope.password,
ConfirmPassword: $scope.confirmPassword,
};
alert("before post");
$http.post(accountUrl, JSON.stringify(userData))
.success(function (data, status, headers, config) {
alert("success");
//$location.path("/login");
}).error(function (data, status, headers, config) {
alert("fail");
$scope.hasRegistrationError = true;
var errorMessage = data.Message;
console.log(data);
}).finally(function () {
});
}
当我调试时,我能够点击API控制器,但我不确定呼叫的移动和结束位置。最后,我在Chrome浏览器控制台中收到错误消息 -
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
app.js:39 ObjectExceptionMessage: "Multiple actions were found that match the request:
↵Logout on type WalmartWebService.Controllers.AccountController
↵ChangePassword on type WalmartWebService.Controllers.AccountController
↵SetPassword on type WalmartWebService.Controllers.AccountController
↵AddExternalLogin on type WalmartWebService.Controllers.AccountController
↵RemoveLogin on type WalmartWebService.Controllers.AccountController
↵Register on type WalmartWebService.Controllers.AccountController
↵RegisterExternal on type WalmartWebService.Controllers.AccountController"ExceptionType: "System.InvalidOperationException"Message: "An error has occurred."StackTrace: " at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext)
↵ at System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext controllerContext)
↵ at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)
↵ at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"__proto__: Object__defineGetter__: __defineGetter__()arguments: nullcaller: nulllength: 2name: "__defineGetter__"__proto__: ()<function scope>__defineSetter__: __defineSetter__()__lookupGetter__: __lookupGetter__()__lookupSetter__: __lookupSetter__()constructor: Object()arguments: nullassign: assign()caller: nullcreate: create()defineProperties: defineProperties()defineProperty: defineProperty()deliverChangeRecords: deliverChangeRecords()freeze: freeze()getNotifier: getNotifier()getOwnPropertyDescriptor: getOwnPropertyDescriptor()getOwnPropertyNames: getOwnPropertyNames()getOwnPropertySymbols: getOwnPropertySymbols()getPrototypeOf: getPrototypeOf()is: is()isExtensible: isExtensible()isFrozen: isFrozen()isSealed: isSealed()keys: keys()length: 1name: "Object"observe: observe()preventExtensions: preventExtensions()prototype: Objectseal: seal()setPrototypeOf: setPrototypeOf()unobserve: unobserve()__proto__: ()<function scope>hasOwnProperty: hasOwnProperty()isPrototypeOf: isPrototypeOf()propertyIsEnumerable: propertyIsEnumerable()toLocaleString: toLocaleString()toString: toString()valueOf: valueOf()get __proto__: get __proto__()arguments: nullcaller: nulllength: 0name: "get __proto__"__proto__: ()<function scope>set __proto__: set __proto__()
然后我在AccountController.cs- RegisterExternal(RegisterExternalBindingModel模型)中评论了自动生成的函数,并在浏览器控制台中得到以下错误 -
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
app.js:26 ObjectExceptionMessage: "Multiple actions were found that match the request:
↵Logout on type WalmartWebService.Controllers.AccountController
↵ChangePassword on type WalmartWebService.Controllers.AccountController
↵SetPassword on type WalmartWebService.Controllers.AccountController
↵AddExternalLogin on type WalmartWebService.Controllers.AccountController
↵RemoveLogin on type WalmartWebService.Controllers.AccountController
↵Register on type WalmartWebService.Controllers.AccountController"ExceptionType: "System.InvalidOperationException"Message: "An error has occurred."StackTrace: " at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext)
↵ at System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext controllerContext)
↵ at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)
↵ at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"__proto__: Object
我是Web API和Angular JS的新手。请帮忙。
谢谢。