如何将2个或更多对象发布到ASP.NET Core WebAPI

时间:2016-12-17 14:13:10

标签: asp.net-core .net-core asp.net-core-webapi asp.net-core-1.1

我正在尝试使用Angular 2将2个对象发布到AspNetCore WebAPI端点。我能够使用正文流成功提取内容。

是否有更优雅的方法来实现这一目标?

[HttpPost]
    [ActionNameAttribute("complex2objects")]
    public User ComplexTwoObjects(){
        var body = Helpers.Request.ExtractBody(this.Request.Body);
        var obj1 = Helpers.Request.GetBodyObject(body,0,new User());
        var obj2 = Helpers.Request.GetBodyObject(body,1,new User());

        return obj2;
    }
    ...

    public static string ExtractBody(Stream body){
        StreamReader reader = new StreamReader(body);
        return reader.ReadToEnd();
    }
    public static T GetBodyObject<T>(string content, int index,T type){
        var composite = JsonConvert.DeserializeObject<IList>(content);
        return JsonConvert.DeserializeAnonymousType(composite[index].ToString(),type);
    }

是否有机会将解析复杂对象卸载到.Net Core / WebAPI?

1 个答案:

答案 0 :(得分:0)

2适用于任何想要从身体中提取多个物体的人的解决方案。请参阅我问题中的最上面一个。或者正如大卫所说,我们可以这样做:

    [HttpPost]
    [ActionNameAttribute("complex2")]
    public User Complex2([FromBody]List<Object> temp){            
        return new User(); 
    }
/*Input JSON Array:
 [{"Username":"inputAAAA","Password":"1234"},{"Property_In_Object2":"123123"}]*/
相关问题