0

I am trying to configure Token based authentication in my Web API for which I want to create a form to user the new users. Below is my code-

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 () {
        });
}

When I debug, I am able to hit the API controller but I am not sure where the call moves and ends. Eventually, I get the error message in my Chrome browser console as-

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__()

Then I commented the auto generated function in AccountController.cs- RegisterExternal(RegisterExternalBindingModel model) and got below error in browser console-

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

I am very new to Web API and Angular JS. Please help.

Thank you.

Souvik Ghosh
  • 4,456
  • 13
  • 56
  • 78
  • You route map probably has a problem. Check http://stackoverflow.com/questions/14534167/multiple-actions-were-found-that-match-the-request-webapi – Parthasarathy Aug 18 '16 at 11:38
  • Can you show the code of other methods (just the signature should be fine). – Ravi A. Aug 18 '16 at 13:54
  • @Sarathy Here is the route which I have in RouteConfig url: "{controller}/{action}/{id}", – Souvik Ghosh Aug 19 '16 at 04:37
  • @RaviA. Sure! Do you want to see the methods from AccountController.cs ? – Souvik Ghosh Aug 19 '16 at 04:38
  • Yes. 1 or 2 methods should be good. – Ravi A. Aug 19 '16 at 05:33
  • @RaviA. Few methods I have added they are comma separated- public async Task Register(RegisterBindingModel model), public async Task RegisterExternal(RegisterExternalBindingModel model), public IEnumerable GetExternalLogins(string returnUrl, bool generateState = false), public async Task GetExternalLogin(string provider, string error = null) – Souvik Ghosh Aug 19 '16 at 05:55
  • Can you prefix your Register method with [HttpPost] and try? – Ravi A. Aug 19 '16 at 11:43

0 Answers0