web api put 识别查询字符串但不识别正文 [英] web api put is recognizing query strings but not body
问题描述
当我将用户作为查询字符串传入(使用 $http 中的参数)并设置 web api 方法以在 uri 中查找它们时,一切都很顺利.但是当我按如下方式传递它们时,用户显示为空.我在这里错过了什么?
when i pass in users as a query string (using params in $http) and set the web api method to look for them in the uri everything is peachy. but when i pass them in as below, users shows up as null. what am i missing here?
角度函数
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 操作
public HttpResponseMessage Put(HttpRequestMessage request, [FromUri]string role, [FromBody]string[] user)
{
return request.CreateResponse(HttpStatusCode.NoContent);
}
推荐答案
Try: data: users
而不是 data: data
.
Try: data: users
instead of data: data
.
在asp.net api中,整个请求体都绑定到一个参数上.因此,您在操作方法参数中不能有多个参数和 [FromBody]
.只有一个 => 我们不需要在请求正文中指定属性名称.
In asp.net api, the whole request body is bound to a parameter. For this reason, you cannot have multiple parameters with the [FromBody]
in the action method parameters. There is only one => we don't need to specify a property name in the request body.
这篇关于web api put 识别查询字符串但不识别正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!