网页API放正在识别查询字符串但不是身体 [英] web api put is recognizing query strings but not body

查看:124
本文介绍了网页API放正在识别查询字符串但不是身体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在用户传递作为查询字符串(使用的$ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆