简体   繁体   English

C# asp.net 核心 webapi 抛出“JSON 值无法转换为 System.Nullable`1[System.DateTime]

[英]C# asp.net core webapi throwing "The JSON value could not be converted to System.Nullable`1[System.DateTime]

I have a asp.net core webapi with class property DateTime?我有一个带有 class 属性 DateTime 的 asp.net 核心 webapi? DateofBirth.出生日期。 The front end is sending the date of birth as a string in the format.前端将出生日期作为格式的字符串发送。 "MM/dd/yyyy". “MM/dd/yyyy”。 I dont want to change the C# property with string type.我不想用字符串类型更改 C# 属性。 Is there a better solution to handle this?有没有更好的解决方案来处理这个问题?

public class Entity
{
    public Guid Id { get; set; }

    [Required(ErrorMessage = "FirstName must be supplied")]
    public string FirstName { get; set; }

    [Required(ErrorMessage = "Surname must be supplied")]
    public string Surname { get; set; }
    
    public DateTime? DateOfBirth { get; set; }
}

POSTMAN request: POSTMAN 请求:

{
    "dateOfBirth": "07/07/2020",
    "surname": "Martin",
    "id": "ae8d7edf-23b6-498b-865d-9840ce401b19",
    "firstName": "Robert",
}

Error:错误:

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "|4dd4b26-4340be36ab0c62cc.",
    "errors": {
        "$.dateOfBirth": [
            "The JSON value could not be converted to System.Nullable`1[System.DateTime]. Path: $.dateOfBirth | LineNumber: 2 | BytePositionInLine: 31."
        ]
    }
}

The error is because by default System.Text.Json library parses the value from JSON according to the ISO 8601:-2019 extended profile.该错误是因为默认情况下System.Text.Json库根据ISO 8601:-2019扩展配置文件解析 JSON 中的值。

Two ways to handle it:两种处理方式:

  1. Change the request of your front end from MM/DD/YYYY to YYYY-MM-DD .将前端的请求从MM/DD/YYYY更改为YYYY-MM-DD (This is ISO 8601 compliant) (这是符合 ISO 8601 的)
  2. Implement custom converter according to this documentation by Microsoft . 根据 Microsoft 的此文档实施自定义转换器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 序列化复杂类型System.Nullable <System.DateTime> - Serialize Complex Type System.Nullable<System.DateTime> JSON 值无法转换为 System.Nullable[System.Int32] - The JSON value could not be converted to System.Nullable[System.Int32] “没有为类型‘System.Nullable`1[System.DateTime]’(参数‘property’)定义属性‘System.DateTime Date’”异常 - “Property 'System.DateTime Date' is not defined for type 'System.Nullable`1[System.DateTime]' (Parameter 'property')” exception 从asp.net(C#)System.dateTime插入/更新SQL DateTime - Insert/Update SQL DateTime From asp.net(C#) System.dateTime 将转储应用到结果时,LINQPad 抛出异常“TruncateTime(System.Nullable`1[System.DateTime])' has no supported translation to SQL” - LINQPad is throwing exception “TruncateTime(System.Nullable`1[System.DateTime])' has no supported translation to SQL” when applying Dump to results ASP.NET Core 3.0 [FromBody] 字符串内容返回“无法将 JSON 值转换为 System.String。” - ASP.NET Core 3.0 [FromBody] string content returns "The JSON value could not be converted to System.String." EF AsNoTracking导致错误。 类型&#39;System.DateTime&#39;的表达式不能用于赋值类型&#39;System.Nullable`1 [System.DateTime]&#39; - EF AsNoTracking Causes error. Expression of type 'System.DateTime' cannot be used for assignment to type 'System.Nullable`1[System.DateTime]' &#39;System.String&#39; 和 &#39;System.Nullable`1[System.DateTime]&#39; 类型之间没有定义强制运算符 - No coercion operator is defined between types 'System.String' and 'System.Nullable`1[System.DateTime]' 不可为空的System.DateTime的值 - Value for Non-Nullable System.DateTime ExecuteMethodCall:类型&#39;System.Nullable`1 [System.DateTime]&#39;必须声明一个默认(无参数)构造函数 - ExecuteMethodCall: The type 'System.Nullable`1[System.DateTime]' must declare a default (parameterless) constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM