ASP.NET Core 2.0 WebAPI序列化复杂类型

时间:2017-10-18 00:49:35

标签: c# json serialization asp.net-core asp.net-core-webapi

ASP.NETCore 2.0 WebAPI控制器有一个方法:

     [HttpGet]
     public IEnumerable<SourceStat> Get()
     {
            return SourceStats.ToList();
     }
     [DataContract]
     public partial class SourceStat: PropertyChangedBase
     {
            [DataMember]
            public long SourceStatId { get; set; }
            [DataMember]
            public int SourceId { get; set; }
            [DataMember]
            public InformationUnitType InformationUnitType { get; set; }
            [DataMember]
            public byte SucceedPercentage { get; set; }
            [DataMember]
            public DateTime Date { get; set; }

            public Source Source { get; set; }
        }

如果我没有使用SourceStat属性标记[DataContract]/[DataMember],则客户端会获得一个空数组。但是我已经看过很多例子,其中[HttpGet]方法返回没有这些属性的复杂对象。为什么[DataContract]/[DataMember]需要我的案子?什么是ASP.NET Core 2.0中使用的默认序列化程序?有没有办法标记除了标记所需属性之外不需要的属性?

0 个答案:

没有答案
相关问题