Mic*_*ael 4 c# asp.net-mvc-4 asp.net-web-api angularjs
当我将用户作为查询字符串传递(使用$ http中的参数)并设置web api方法以在uri中查找它们时,一切都很好.但是当我按照下面的方式传递它们时,用户显示为null.我在这里失踪了什么?
角度函数
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 });
});
};
Run Code Online (Sandbox Code Playgroud)
web api行动
public HttpResponseMessage Put(HttpRequestMessage request, [FromUri]string role, [FromBody]string[] user)
{
return request.CreateResponse(HttpStatusCode.NoContent);
}
Run Code Online (Sandbox Code Playgroud)
尝试:data: users而不是data: data.
在asp.net api中,整个请求体绑定到一个参数.出于这个原因,你不能有多个参数用[FromBody]在动作方法的参数.只有一个=>我们不需要在请求体中指定属性名称.