4

when i pass in users as a query string (using params in $http) and set the web api method to look for them in the uri everything is peachy. but when i pass them in as below, users shows up as null. what am i missing here?

angular function

scope.saveChanges = function () {
        // create array of user id's
        var users = [];
        angular.forEach(scope.usersInRole, function (v, k) {
            users.push(v.Key);
        });

        var data = { user: users };

        var token = angular.element("input[name='__RequestVerificationToken']").val();

        // put changes on server
        http({
            url: config.root + 'api/Roles/' + scope.selectedRole + '/Users',
            method: 'PUT',
            data: data,
            contentType: "application/json; charset=utf-8",
            headers: { "X-XSRF-Token": token },
            xsrfCookieName: '__RequestVerificationToken'
        }).success(function (result) {
            // notify user changes were saved
            angular.element('#myModal').reveal({ closeOnBackgroundClick: false });
        });
    };

web api action

 public HttpResponseMessage Put(HttpRequestMessage request, [FromUri]string role, [FromBody]string[] user)
            {

                return request.CreateResponse(HttpStatusCode.NoContent);
            }
3
  • 1
    try: data: users instead of data: data Commented Feb 7, 2014 at 3:00
  • well i'll be the son of a monkey's uncle. so simple. put that in an answer so i can mark it and give you credit. Commented Feb 7, 2014 at 3:02
  • i am curious though as to why just passing in the array to data works so cleanly and passing in how i have it doesn't. Commented Feb 7, 2014 at 3:03

1 Answer 1

7

Try: data: users instead of data: data.

In asp.net api, the whole request body is bound to a parameter. For this reason, you cannot have multiple parameters with the [FromBody] in the action method parameters. There is only one => we don't need to specify a property name in the request body.

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.