0

I have one application consist of ASP.Net WebAPI 2 and Web project for which angularJS is used. The Problem is when i call API with parameters, it does not hit. and when send object, it gets hit.

anjularJS Code:

with parameters

$http({
    cache: false
        , method: 'POST'
        , url: 'http://localhost:51341/api/User/UpdateUser'
        , data: { userId: $scope.UsersId, fullName: $scope.name }
}).success(function (data, status, headers, config) {
})
.error(function (data, status, headers, config) {
});

Webapi Code

[HttpPost]
public ApiResponse UpdateUser(int userId, string fullName)
{
    return this.Response(true, MessageTypes.Success, "User has been saved successfully.");
}

with object

var model = { userId: $scope.UsersId, fullName: $scope.name };
$http({
    cache: false
        , method: 'POST'
        , url: 'http://localhost:51341/api/User/UpdateUser'
        , data: model
}).success(function (data, status, headers, config) {
})
.error(function (data, status, headers, config) {
});

webapi code

[HttpPost]
public ApiResponse UpdateUser(User model)
{
    return this.Response(true, MessageTypes.Success, "User has been saved successfully.");
}

User class

public class User
{
    public int UserId { get; set; }
    public string FullName { get; set; }
    public int Age { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
}

when call made with parameters, api DOES NOT get hit. but When call made with Object, api GETS hit.

What I missed when call made with parameters ? Help will be truly appreciated.

Mihir Shah
  • 948
  • 10
  • 17

1 Answers1

1

From WebApi docs:

At most one parameter is allowed to read from the message body. So this will not work:

// Caution: Will not work!    
public HttpResponseMessage Post([FromBody] int id, [FromBody] string name) { ... }

For other info I suggest to read:

https://damienbod.wordpress.com/2014/08/22/web-api-2-exploring-parameter-binding/ https://stackoverflow.com/a/24629106/3316654

Community
  • 1
  • 1
wilver
  • 2,106
  • 1
  • 19
  • 26