Web api put recognizes query strings but not body

when I pass users as a query string (using the parameters in $ http) and set the web api method to find them in uri, everything is peach. but when I pass them as shown below, users are displayed 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);
            }
+4
source share
1 answer

Try: data: usersinstead data: data.

asp.net api . [FromBody] . = > .

+7

Source: https://habr.com/ru/post/1525728/


All Articles