我有归结为一些非常基本的HttpClient代码:网络API后复杂,对象与泛型列表,物业,计数0
var criteria = new Criteria() { Name = "TestName" };
criteria.listProperty.Add(new ComplexObject<int>("value", true));
httpClient.PostAsJsonAsync("api/ctl/myAction", criteria)
控制器看起来是这样的:
[HttpPost]
public HttpResponseMessage myAction([FromBody]Criteria criteria)
{
return DoSomethingWithIt(criteria);
}
事情是,我在我的PostAsJsonAsync
上放了一个断点,并且我的标准对象具有名称“TestName”,并且其listProperty具有一个带有“值”和真实属性的项目。所有事情都应该如此。
但控制器上的断点显示标准具有计数0,但仍显示标准对象的名称为“TestName”。然后我试了一下:
[HttpPost]
public HttpResponseMessage myAction(Object criteria)
{
var jsonString = model.ToString();
}
而且jsonString具有一切,包括具有“value”和true属性的复杂属性。
我的标准和ComplexObject对象是像这样:
public class SearchCriteria
{
public List<ComplexObject> listProperty { get; set; }
public string Name { get; set; }
}
public class ComplexObject<T> : ComplexObject
{
public T Value { get; set; }
public List<T> Choices { get; private set; }
public ComplexObject<T>(string complexName, bool isRequired, List<T> choices = null)
{
this.ComplexName = complexName;
this.IsRequired = isRequired;
this.Choices = choices;
}
}
public abstract class ComplexObject
{
public string ComplexName { get; protected set; }
public bool IsRequired { get; protected set; }
}
P.S:我已经试过两个控制器使用和不使用[FromBody。