簡體   English   中英

如何將2個或更多對象發布到ASP.NET Core WebAPI

[英]How to POST 2 or more objects to ASP.NET Core WebAPI

我正在嘗試使用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?

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"}]*/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM