POST请求中发送的对象内的JSON对象未传递给C#方法

时间:2019-01-16 10:07:57

标签: c# .net json null frombodyattribute

我正在尝试通过发布请求将参数传递给C#方法。我有一个C#对象,其中包含我随请求发送的所有字段,其中的一个是我创建的非原始对象。当我以JSON表示法发送带有对象的POST请求时,所有其他字段均正确传递,但其中一个-变为空。

我正在使用[FromBody]属性,因此我尝试不使用它,但该属性仍然为null。

这是我的JSON,其中我的字段名称被剪掉:

{
    "cId": 12,
    "startDate": "2017-01-01 12:00:00",
    "endDate": "2018-01-01 19:00:00",
    "isActive": 1,
    "cType": 1,
    "targetItemCount": 10,
    "targetPayedCount": 100,
    "uses": 50,
    "items": [10, 11, 12],
        "info":
             {
            "InfoId": "00001273-9018-401b-92c1-729a3f895c0f",
            "Barcode": "123123",
            "Status": null,
            "ImageUrl": "urll",
            "Name": "name",
            "Text1": "text1",
            "Text2": "text2",
            "DiscountText": "distext",
            "Price": 10
        } 
}

我的方法签名是:

 [HttpPost]
        public bool DaMethod([FromBody]DaObject Da)

DaObject类:

public class DaObject
{
    public int cId { get; set; }
    public string startDate { get; set; } 
    public string endDate { get; set; } 
    public bool isActive { get; set; }
    public short cType { get; set; }
    public short targetItemCount { get; set; }
    public short targetPayedCount { get; set; }
    public int uses { get; set; }
    public List<int> items { get; set; }
    public Info info { get; set; }
}

里面的Info对象看起来像这样:

[Serializable]
public class Info
{
    [DataMember]
    public Guid InfoId { get; set; }
    [DataMember]
    public string Barcode { get; set; }
    [DataMember]
    public eStatus Status { get; set; }
    [DataMember]
    public string ImageUrl { get; set; }
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public string Text1 { get; set; }
    [DataMember]
    public string Text2 { get; set; }
    [DataMember]
    public string DiscountText { get; set; }
    [DataMember]
    public string Price { get; set; }
}

我不断在内部对象的所有字段中获取空值。尽管DaObject中的其余字段都传递了实际值。我该如何解决?

0 个答案:

没有答案