Angular and WebAPI Method Parameter Always Zero

I have an application that suddenly stopped working. I can’t understand why this is so, as far as I know, it should work. I must be missing something very simple here. This is the code, the Angular code calls the API, and the API method calls the call, but the method parameter is always zero. A call is a call to PUT.

public static void Register(HttpConfiguration config)
{
    // Web API configuration and services

    // Web API routes
    config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{value}",
        defaults: new { value = RouteParameter.Optional }
    );
}

What is my route just in case its a problem. But I do not think that since the method is actually called, just the parameter is not passed.

[Authorize]
public class UsersController : ApiController
{
    // PUT api/users/undelete
    [HttpPut]
    public void Undelete(long? value)
    {
        if (!value.HasValue)
        {
            throw new ArgumentNullException("[UsersController => Undelete(long? value)]");
        }

        new UsersBoundary(this.repository).UndeleteUser(value.Value);
    }

    public UsersController(IUsersRepository repository)
    {
        this.repository = repository;
    }

    public UsersController() : this(new UsersRepository()) {  }

    private readonly IUsersRepository repository;
}

This is the user controller for WebAPI. Undelete method calls, but long? the value is always null.

(function () {
    'use strict';

    var app = angular.module('adminpowertools');

    var HomeController = function ($scope, homeService) {

        $scope.searchString = '';
        $scope.currentPage = 1;

        $scope.undelete = function(user) {
            homeService.undeleteUser(user.recordId);
            angular.forEach($scope.users, function(value, key) {
                if (value.recordId === user.recordId) {
                    value.deleted = 0;
                }
            });
        };
    };

    app.controller('HomeController', ['$scope', 'HomeService', HomeController]);

}());

This my controller is again very simple again, and I do not think that something is broken here, but I turn it on for completeness.

(function () {
    'use strict';

    var app = angular.module('adminpowertools');

    var HomeService = function ($http) {

        var undeleteUser = function(userId) {
            var uri = 'api/users/undelete';
            var data = { value: userId };

            return $http.put(uri, JSON.stringify(data)).success(function(data, status, headers, config) {
                return true;
            });
        };

        return {
            undeleteUser: undeleteUser
        };
    };

    app.factory('HomeService', ['$http', HomeService]);
}());

, . , , . value = = + userId, , .

. .

+4
1

( ) , Uri. Web Api URI , .

Angular params "data" $http. . , $http, .

var undeleteUser = function(userId) {
        var config = {
            method: 'PUT',
            url: 'api/users/undelete',
            params: { value: userId }
        };            

        return $http(config).success(function(data, status, headers, config) {
            return true;
        });
    };
+5

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


All Articles