我有一个像这样的单独的Enum类:
[JsonConverter(typeof(StringEnumConverter))]
public enum AStatus
{
[EnumMember(Value = "Unverified")]
Unverified = 0,
[EnumMember(Value = "Verified")]
Verified = 1,
[EnumMember(Value = "Error")]
Error = 2,
[EnumMember(Value = "ServiceDown")]
ServiceDown = 3,
}
我正在这样使用它:
[DataContract]
public partial class ResultDto
{
[DataMember(Name="status", EmitDefaultValue=false)]
public AStatus Status { get; set; }
[DataMember(Name = "someOtherObjectList ", EmitDefaultValue = false)]
public List<SomeOtherObject> SomeOtherObjectList { get; set; }
[DataMember(Name = "messages", EmitDefaultValue = false)]
public string Messages { get; set; }
public string ToJson()
{
return JsonConvert.SerializeObject(this, Formatting.Indented);
}
}
如果我附加到呼叫,直到确定(ok),我都具有所有属性,但是当我查看浏览器的网络环境时,Enum属性丢失了,这意味着它根本不在resultDto中。
[HttpPost]
[Route("/someoperation")]
[ValidateModelState]
[SwaggerOperation("Check")]
[SwaggerResponse(200, typeof(List<ResultDto>), "successful operation")]
[SwaggerResponse(400, typeof(List<ResultDto>), "Invalid")]
public virtual IActionResult Check([FromBody] RequestDto requestDto)
{
var res = _aService.Check(requestDto);
return Ok(res); // until here response object has enum and it's value
}
答案 0 :(得分:0)
如果要使用Enum的默认值,则删除EmitDefaultValue = false,否则,必须设置Status的值
[DataMember(Name="status")]
public AStatus Status { get; set; }
答案 1 :(得分:0)
启用空值处理
services.AddMvc(options =>
{...}).AddJsonOptions(jsonOptions =>
{
jsonOptions.SerializerSettings.Converters.Add(new StringEnumConverter());
jsonOptions.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
jsonOptions.SerializerSettings.NullValueHandling = NullValueHandling.Include;
})