1

I know it's been asked before, and I've tried figuring it out myself (and failed). So here's my code:

...
$resource("/SWDB/api/admin/users/:id", { id: "@id" },
        {
            update: {
                method: "PUT"
            }
        }
...

And

...
return AdminResource.users.update(params).$promise;
...

This is how my update is called:

$scope.params = {
        id: $stateParams.id,
        email: $scope.userservice.selectedUser.email,
        oldPassword: $scope.oldPassword,
        newPassword: $scope.newPassword,
        isLockoutEnabled: $scope.userservice.selectedUser.lockoutEnabled
    };

    $scope.userservice.updateUser($scope.params)
            .then(function() { $state.go("/"); }, function(error) { console.log(error) });

And finally, this is my controller method:

public async Task<HttpResponseMessage> Put(string id, string email, 
            string oldPassword, string newPassword, bool isLockoutEnabled)
        {
            var user = await _owinManager.UserManager.FindByIdAsync(id);
            if (user != null)
            {
                user.Email = email;

                if (!string.IsNullOrEmpty(oldPassword))
                {
                    if (_owinManager.UserManager.PasswordHasher.HashPassword(oldPassword) == user.PasswordHash)
                    {
                        if (!string.IsNullOrEmpty(newPassword))
                        {
                            user.PasswordHash = _owinManager.UserManager.PasswordHasher.HashPassword(newPassword);
                        }
                    }
                }

                var result = await _owinManager.UserManager.UpdateAsync(user);
                if (result.Succeeded)
                {
                    return new HttpResponseMessage(HttpStatusCode.Accepted);
                }
            }

            return new HttpResponseMessage(HttpStatusCode.InternalServerError);
        }

I keep getting: 405, not allowed. I tried POST, same thing. My guess is I am making an erroneous call... I just don't know where the error is :(

2
  • I would pass a model instead of single parameters - this way you can leverage web api model binding. Commented Dec 20, 2015 at 14:36
  • I have only just done that, and it worked - apparently binding works, whereas single parameters don't. My guess is, angular sends params over as json and that helps? Commented Dec 20, 2015 at 14:49

1 Answer 1

1

I think the problem is because the Web API Controller define 5 params, but, angularjs is sending 1 (yes, a complex parameter), so, in your web api change the five params to a complex parameter like this:

public class Test
{
    public string Id {get; set;}
    public string Email {get; set;}
    public string OldPassword {get; set;}
    public string NewPassword {get; set;}
    public bool IsLockoutEnabled {get; set;}
}

public async Task<HttpResponseMessage> Put(Test entity)
{
    .....
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.