网页API放正在识别查询字符串但不是身体 [英] web api put is recognizing query strings but not body
问题描述
当我在用户传递作为查询字符串(使用的$ HTTP PARAMS),并设置Web API方法寻找他们的URI一切桃色的。但是当我在下面通过他们,用户显示为空。我失去了什么吗?
角功能
scope.saveChanges =功能(){
//创建用户ID的数组
VAR用户= [];
angular.forEach(scope.usersInRole,函数(V,K){
users.push(v.Key);
}); VAR数据= {用户:用户}; VAR令牌= angular.element(输入[名称= __ RequestVerificationToken'])VAL()。 //把服务器更改
HTTP({
网址:config.root +'API /角色/'+ scope.selectedRole +'/用户的,
方法:把',
数据:数据,
的contentType:应用/ JSON的;字符集= UTF-8,
标题:{X-XSRF令牌:令牌},
xsrfCookieName:__RequestVerificationToken
})。成功(功能(结果){
//通知用户更改已保存
。angular.element('#myModal)显示({closeOnBackgroundClick:假});
});
};
网页API行动
公开的Htt presponseMessage把(HTT prequestMessage要求,[FromUri]串角色,[FromBody]字符串[]用户)
{ 返回request.CreateResponse(的HTTPStatus code.NoContent);
}
尝试:数据:用户
而不是数据:数据
。
在asp.net API, [FromBody]
。只有一个=>我们不需要在请求主体以指定一个属性名称
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?
angular function
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 action
public HttpResponseMessage Put(HttpRequestMessage request, [FromUri]string role, [FromBody]string[] user)
{
return request.CreateResponse(HttpStatusCode.NoContent);
}
Try: data: users
instead of data: data
.
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.
这篇关于网页API放正在识别查询字符串但不是身体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!