在我的 REST API 中,我的模型绑定没有映射作为对象列表的属性。它正确地到达控制器,并显示所有属性,但它们是空的。
例如,对于 POST 请求{age: 202, setting: ["school"]}
,我在响应中得到如下内容:
Got param type=SearchStringDTO; param={"age":202,"setting":[]}
我希望它回应这样的事情:
Got param type=SearchStringDTO; param={"age":202,"setting":["school":true, "hospital":false]}
如何引导它将设置参数解析为List<Setting>
?
这是控制器:
using System.Web.Http;
...
[HttpPost]
public HttpResponseMessage Index(SearchStringDTO param) {
return ResponseMessage(this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Got param type= " + param.GetType() + "; param = " + JsonConvert.SerializeObject(param)));
}
这是模型:
public sealed class SearchStringDTO {
public int age { get; set; }
public List<Setting> setting { get; set; }
}
这是设置类:
public class Setting {
public bool hospital { get; set; }
public bool school { get; set; }
}
我开始沿着这条路径手动解析事物,但这是 JObject、JToken、JArray 的噩梦。
[HttpPost]
public IHttpActionResult Index(Newtonsoft.Json.Linq.JObject param) {
return ResponseMessage(this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Got param type= " + param.GetType() + "; param = " + JsonConvert.SerializeObject(param)));
}
也许我需要一个自定义绑定模型?我迷失了试图弄清楚如何构建和使用一个。