【问题标题】:Modify ASP.NET Web API deserialization process修改 ASP.NET Web API 反序列化过程
【发布时间】:2016-09-28 11:54:12
【问题描述】:

我创建了一个基于 OWIN / Katana 的 Web 服务器。目前我在理解 JSON 反序列化过程方面存在问题...

这是简单的 POST 方法:

public IHttpActionResult Post([FromBody] Person person) {
// do some stuff
return Ok();
}

现在的问题是:为什么默认调用模型的所有 Getter,如果有属性为“JsonIgnore”或没有“DataMember”。

型号:

[DataContract]
public class Person
{
    private string firstName;

    //This property-getter should not be called
    public string FirstName{
        get {
            return firstName;
        }
        set {
            firstName = value;
        }
    }

    //This property-getter should be called
    [DataMember]
    public string LastName { get;set; }
}

可以改变这个过程吗?

【问题讨论】:

  • 如果您将使用属性 JsonIgnore 装饰 FirstName 属性 - 在序列化/反序列化时不会调用 getter。
  • Getter 仍然被调用,同样带有 JsonIgnore 属性

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


【解决方案1】:

解决了。

我检测到在 Web API 模型验证器期间调用了 getter(请参阅类“DefaultBodyModelValidator”)

【讨论】:

    猜你喜欢
    • 2014-04-27
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 2017-07-11
    • 1970-01-01
    相关资源
    最近更新 更多