4

I have an application that all of a sudden stopped working. I cannot determine why this is as far as I know it should be working. I must be missing something very simple here. This is the code, the Angular code call the API and the API method does get call but the parameter to the method is always null. The call is a PUT call.

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 }
    );
}

That's my route just in case its the problem. But I don't think so since the method does get called just the parameter is not getting passed in.

[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;
}

That is the Users Controller for the WebAPI nothing fancy here. The Undelete method does get called but the long? value parameter 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 is my controller again very simple and I don't think anything here is broken but I am including it 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]);
}());

This is the service that calls the controller. Again it does call the controller just does not pass in the value that's set in the data variable. I have tried setting data to 'value=' + userId, I tried with double quotes and single quotes and no quotes nothing seems to work.

Any help would be greatly appreciated. Thank you.

6
  • 1
    try $http.put(uri, JSON.stringify(data)) should work Commented Apr 21, 2015 at 15:09
  • 'value='? You mean value, don't you? Commented Apr 21, 2015 at 16:03
  • JSON.stringify(data) didn't help the value that is passed in is still null. I can see in the debugger that I am passing in an actual value of 1989 if just doesn't come out the other end. :) Commented Apr 21, 2015 at 17:41
  • I also changed the 'value' to value. No change. Commented Apr 21, 2015 at 17:53
  • @Lukasz do you have any other route? Commented Apr 21, 2015 at 17:59

1 Answer 1

5

You are passing the data (value in this case) via the body of the message rather than the Uri. By default, Web Api serializes the URI for query string parameters into variables on a particular method, not the body.

Change your Angular call to send value parameters via "params" property versus "data" parameter to $http. Note I refactored it into a config object to pass into $http to make it cleaner.

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;
        });
    };
Sign up to request clarification or add additional context in comments.

2 Comments

This has fixed it. Thank You!
hey chris.. bless you...your answer saved my time and energy.

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.