C# 继承的类和属性未包含在ASP.NET核心API的JSON结果中
我有一个简单的模型,我尝试将其序列化为JSON,但结果中没有包含一些属性。当我的对象从基类继承时,基类的属性不会出现在json中 在JSON字符串中,“searchModel”是{} SearchModelBase.cs:C# 继承的类和属性未包含在ASP.NET核心API的JSON结果中,c#,json,asp.net-core,asp.net-apicontroller,C#,Json,Asp.net Core,Asp.net Apicontroller,我有一个简单的模型,我尝试将其序列化为JSON,但结果中没有包含一些属性。当我的对象从基类继承时,基类的属性不会出现在json中 在JSON字符串中,“searchModel”是{} SearchModelBase.cs: public interface ISearchModelBase { SearchTypes Type { get; set; } string SearchString { get; set; } } public abstract class Sear
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
}
public interface IAssetsDefaultSearchModel : ISearchModelBase
{
}
public class AssetsDefaultSearchModel : SearchModelBase, IAssetsDefaultSearchModel
{
public AssetsDefaultSearchModel(SearchTypes type, string searchString) : base(type, searchString)
{
}
}
{
"items": [
{
"displayName": "FFU Samarin",
"data": {
"appId": 3,
"displayName": "FFU Samarin",
..........
资产默认搜索模型.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
}
public interface IAssetsDefaultSearchModel : ISearchModelBase
{
}
public class AssetsDefaultSearchModel : SearchModelBase, IAssetsDefaultSearchModel
{
public AssetsDefaultSearchModel(SearchTypes type, string searchString) : base(type, searchString)
{
}
}
{
"items": [
{
"displayName": "FFU Samarin",
"data": {
"appId": 3,
"displayName": "FFU Samarin",
..........
JSON:
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
}
public interface IAssetsDefaultSearchModel : ISearchModelBase
{
}
public class AssetsDefaultSearchModel : SearchModelBase, IAssetsDefaultSearchModel
{
public AssetsDefaultSearchModel(SearchTypes type, string searchString) : base(type, searchString)
{
}
}
{
"items": [
{
"displayName": "FFU Samarin",
"data": {
"appId": 3,
"displayName": "FFU Samarin",
..........
在Visual Studio中,集合中的每个项都包含AssetDefaultSearchModel,两个属性中都有值:
在从ISearchResultBase派生的接口中,我必须使用new关键字定义一个新属性。然后,我返回一个从IAssetDefaultSearchResult派生的对象(不复制类中的属性),它按预期工作
public interface IAssetsDefaultSearchResult : ISearchResultBase
{
new ISearchModelBase SearchModel { get; }
string DisplayName { get; }
object Data { get; }
TagBuilder HtmlTag { get; }
}
将以下代码添加到Startup.cs
services.AddControllers().AddNewtonsoftJson();
您可以使用NuGet软件包这是.net-core-3吗?您正在使用哪个序列化库?