【问题标题】:JsonConverter does not work on model bindingJsonConverter 不适用于模型绑定
【发布时间】:2019-07-03 14:46:12
【问题描述】:

我有以下型号

public class SignDocumentsModel
{
    [JsonProperty(ItemConverterType = typeof(BinaryConverter))]
    public byte[][] Documents { get; set; }

    public bool Detached { get; set; }
}

和控制器代码

[HttpPost]
[Route("{requestId}/sign")]
public Task<IHttpActionResult> SignDocuments([FromUri] Guid requestId, SignDocumentsModel parameters)
{
    return SomeKindOfProcessing(requestGuid, parameters);
}

现在,当我向邮递员执行请求时

POST
Content-Type: application/json
{
    "Detached": "true",
    "Documents": [
        "bG9weXN5c3RlbQ=="
    ]
}

我想 Documents 属性应该填充从请求内容中发布的 Base64 字符串解码的字节数组,尽管实际上该属性是空的(如果它在模型中的类型是 List&lt;byte[]&gt;byte[][]null如果是IEnumerable&lt;byte[]&gt;)。

为什么在模型绑定期间没有在请求正文反序列化时调用 JsonConverter?怎么解决?

【问题讨论】:

    标签: c# asp.net-web-api json.net asp.net-web-api2


    【解决方案1】:

    你试过删除[JsonProperty(ItemConverterType = typeof(BinaryConverter))]吗?

    在我的测试设置中,模型在我删除该属性后成功绑定。

    编辑:更多信息...

    根据Json.NET Serialization Guide,默认情况下byte[] 将序列化为base64 字符串。从source code 来看,看起来BinaryConverter 应该与System.Data.Linq.BinarySystem.Data.SqlTypes.SqlBinary 一起使用——而不是byte[]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-23
      • 1970-01-01
      • 2018-06-19
      • 2015-06-27
      • 1970-01-01
      • 2016-03-30
      • 1970-01-01
      相关资源
      最近更新 更多