将多个对象从Angular控制器发布到Web API 2 [英] POST multiple objects from Angular controller to Web API 2

查看:108
本文介绍了将多个对象从Angular控制器发布到Web API 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够从我的角度控制器发送原始的json对象,该对象在我的Web api方法中反序列化为已知类型.很好,但是我现在需要能够在同一请求中发送其他参数,这些参数可以是json对象或简单类型,例如string或int.

I am able to send a raw json object from my angular controller which is deserialized to a known type at my web api method. This is great but I now need to be able to send other parameters in the same request, these could be json objects or simple types like string or int.

我看过诸如

I've seen articles such as this which describe exactly my problem but they are sending their request from codebehind rather than client side.

我试图构造一个json数组并将其发送进去,但收到以下消息:无法创建抽象类" .

I tried to construct a json array and send this in but I get the following message : 'Cannot create an abstract class'.

控制器代码(已修改)

var request = {
            params:[]
        };

        request.params.push(jsonObjectA);
        request.params.push({"someString" : "ABCDEF"});

        $http({
            method: 'POST',
            url: targetUri,
            data: request
        })

Web API方法签名

 public JsonResult TestMethod(JArray request)

这种方法听起来合理吗?如果可以的话,我真的想避免为每个请求创建dto对象.

Does this approach sound sensible? I really want to avoid having to create dto objects for every request if I can.

推荐答案

OK通过遵循然后,我能够在api方法签名中使用简单和复杂类型:

Then I was able to use simple and complex types in my api method signatures :

public JsonResult MyAction(StonglyTypedObject myObj, string description)

我正在传递data参数中的值:

and I am passing in the values in the data parameter :

 $http({
            method: 'POST',
            url: targetUri,
            data: {
                myObj: myObj,
                description: "desc here"
            }
        }).success(...

这是我能找到的最干净的解决方案,希望对您有帮助.

This was the cleanest solution I could find, hope it helps you too.

这篇关于将多个对象从Angular控制器发布到Web API 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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