【发布时间】:2017-05-03 03:20:13
【问题描述】:
我正在尝试使用 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?
【问题讨论】:
-
POST 数据是什么样的?
-
[{"prop1":1234"}, {"prop2_Of_SecondObject":"1234"}]
-
我相信模型绑定器会为您完成艰苦的工作。您应该能够向您的操作添加一个参数,该参数是用户的数组/IEnumerable。
-
你说得对,我们可以做这样的事情。有用。
public User Complex2([FromBody]List<Object> temp){ return null; }并通过 temp[0]、temp[1] 等访问它们...
标签: asp.net-core .net-core asp.net-core-webapi asp.net-core-1.1