【发布时间】:2019-12-19 07:14:16
【问题描述】:
我有一个简单的模型,我尝试将其序列化为 JSON,但某些属性未包含在结果中。当我的对象继承自基类时,基类的属性不会出现在 json 中。
在 JSON 字符串中“searchModel”是 {}
SearchModelBase.cs:
public interface ISearchModelBase
{
SearchTypes Type { get; set; }
string SearchString { get; set; }
}
public abstract class SearchModelBase : ISearchModelBase
{
public SearchModelBase(SearchTypes type, string searchString)
{
this.Type = type;
this.SearchString = searchString;
}
public SearchTypes Type { get; set; }
public string SearchString { get; set; }
}
public enum SearchTypes
{
User,
Site
}
AssetsDefaultSearchModel.cs:
public interface IAssetsDefaultSearchModel : ISearchModelBase
{
}
public class AssetsDefaultSearchModel : SearchModelBase, IAssetsDefaultSearchModel
{
public AssetsDefaultSearchModel(SearchTypes type, string searchString) : base(type, searchString)
{
}
}
JSON:
{
"items": [
{
"displayName": "FFU Samarin",
"data": {
"appId": 3,
"displayName": "FFU Samarin",
..........
在 Visual Studio 中,集合中的每个项目都包含 AssetsDefaultSearchModel,并且在两个属性中都有值:
【问题讨论】:
-
这是 .net-core-3 吗?您使用的是哪个序列化库?
标签: c# json asp.net-core asp.net-apicontroller