【发布时间】:2018-04-20 05:30:36
【问题描述】:
[Table("LegalEntity")]
[ModelMetadataType(typeof(LegalEntityMeta))]
public class LegalEntity : Entity<long>
{
}
public class LegalEntityMeta
{
[JsonProperty(PropertyName = "LegalEntityId")]
public long Id { get; set; }
[JsonProperty(PropertyName = "LegalEntityName")]
public string Name { get; set; }
}
在 Startup.cs ....
services
.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
})
.AddAutoMapper(typeof(Startup))
.AddMvcCore()
.AddJsonFormatters()
.AddApiExplorer();
我的期望是看到带有 legalEntityId 和 legalEntityName 属性的 json,但生成的 json 具有 id 和 name 作为属性。 有人可以帮助我如何更改 json 属性吗? 谢谢 阿南德
【问题讨论】:
-
LegalEntity没有Id或Name属性。那些属于基类吗? -
是的,那些是从基类继承而来的
-
在黑暗中拍摄,因为我个人不使用它,但如果没记错的话,我认为您申请
ModelMetadataType的课程必须是partial。 -
还值得一提的是,该属性的存在主要是为了支持验证。序列化时,Json.Net 完全有可能根本不注意它。
-
如果它不是派生类并且 id 和 name 属性将被重命名为 JsonProperty 属性。如果不使用 ModelMetadataType,我希望 Json.Net 能够以其他方式支持派生属性的 JsonProperty - 否则这将是一个不可能的大洞
标签: c# json.net asp.net-core-2.0 data-annotations