问题描述
考虑以下 JSONConverter(来自 System.Text.Json
)和标记的相关行:
public class MyDateTimeConverter : JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader,Type typeToConvert,JsonSerializerOptions options)
{
throw new JsonException("Error during Read"); // <====================== relevant line
Debug.WriteLine("JsonConverter read");
Debug.Assert(typeToConvert == typeof(DateTime));
return DateTime.Parse(reader.GetString());
}
public override void Write(Utf8JsonWriter writer,DateTime value,JsonSerializerOptions options)
{
Debug.WriteLine("JsonConverter write");
writer.WriteStringValue(value.ToString());
}
}
模型引用了 MyDateTimeConverter
:
public class PostRequest
{
[ModelBinder(BinderType = typeof(DateTimeModelBinder))]
[JsonConverter(typeof(MyDateTimeConverter))]
}
当我现在向我的控制器发布一些内容时,正如预期的那样,我收到一个错误,但密钥不是 $.Date
,而是 Date
:
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1","title": "One or more validation errors occurred.","status": 400,"traceId": "<removed>","errors": {
"$.Date": [ // <============== Note that it is "$.Date" and not only "Date"
"Error during Read"
]
}
}
我理解为什么会发生这种情况($.Date
是 JSON 路径),但我想知道是否可以将其更改为仅 Date
(或模型中的任何属性名称)?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)