0

2つのオブジェクトをAngular 2を使用してAspNetCore WebAPIエンドポイントにPOSTしようとしています。ボディストリームを使用してコンテンツを正常に抽出できました。ASP.NET Core WebAPIに2つ以上のオブジェクトをPOSTする方法

これを実現するより洗練された方法がありますか?

[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にオフロードする機会はありますか?

+0

POSTデータはどのように見えますか? –

+0

[{"" prop1 ":1234"}、{"prop2_Of_SecondObject": "1234"}] – Tony

+2

モデルバインダーがあなたのために苦労していると確信しています。配列ユーザーの/ IEnumerableを –

答えて

0

2体から複数のオブジェクトを抽出するためのソリューション。いずれか私の質問のトップ1を参照してください。デビッドが示唆したように、私たちは次のようなことをすることができます:

[HttpPost] 
    [ActionNameAttribute("complex2")] 
    public User Complex2([FromBody]List<Object> temp){    
     return new User(); 
    } 
/*Input JSON Array: 
[{"Username":"inputAAAA","Password":"1234"},{"Property_In_Object2":"123123"}]*/ 
関連する問題