Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

web api put is recognizing query strings but not body

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);
            }
like image 675
Michael Avatar asked Mar 20 '23 16:03

Michael


1 Answers

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.

like image 106
Khanh TO Avatar answered Apr 06 '23 18:04

Khanh TO