发送单个参数时,不能命中动作结果。发送对象时,它被击中
When send individual parameter, action result cannot be hit. when send object, it gets hit
我有一个应用程序由 ASP.Net WebAPI 2 和使用 angularJS 的 Web 项目组成。问题是当我用参数调用 API 时,它没有命中。当发送对象时,它被命中。
anjularJS代码:
带参数
$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代码
[HttpPost]
public ApiResponse UpdateUser(int userId, string fullName)
{
return this.Response(true, MessageTypes.Success, "User has been saved successfully.");
}
有对象
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) {
});
网页api代码
[HttpPost]
public ApiResponse UpdateUser(User model)
{
return this.Response(true, MessageTypes.Success, "User has been saved successfully.");
}
用户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; }
}
当使用参数进行调用时,api 不会被命中。但是当使用 Object 进行调用时,api GETS 命中。
使用参数调用时我错过了什么?我们将不胜感激。
来自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) { ... }
有关我建议阅读的其他信息:
https://damienbod.wordpress.com/2014/08/22/web-api-2-exploring-parameter-binding/
我有一个应用程序由 ASP.Net WebAPI 2 和使用 angularJS 的 Web 项目组成。问题是当我用参数调用 API 时,它没有命中。当发送对象时,它被命中。
anjularJS代码:
带参数
$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代码
[HttpPost]
public ApiResponse UpdateUser(int userId, string fullName)
{
return this.Response(true, MessageTypes.Success, "User has been saved successfully.");
}
有对象
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) {
});
网页api代码
[HttpPost]
public ApiResponse UpdateUser(User model)
{
return this.Response(true, MessageTypes.Success, "User has been saved successfully.");
}
用户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; }
}
当使用参数进行调用时,api 不会被命中。但是当使用 Object 进行调用时,api GETS 命中。
使用参数调用时我错过了什么?我们将不胜感激。
来自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) { ... }
有关我建议阅读的其他信息:
https://damienbod.wordpress.com/2014/08/22/web-api-2-exploring-parameter-binding/