【问题标题】:Webapi. Define default Json value when enum property is missing网络API。缺少枚举属性时定义默认 Json 值
【发布时间】:2015-03-04 00:38:10
【问题描述】:

我有一个 webapi 接受 Post 如下(示例)

public Foo Post(MyInfo info)
        {
            return new Foo();
        }

MyInfo 可能是这样的

public class MyInfo 
    {
        [JsonProperty(PropertyName = "n")]
        public int MyInt1{ get; set; }

        [JsonProperty(PropertyName = "v")]
        public string MyString1{ get; set; }
}

当我的桌面应用程序(版本 1)向此 webapi 发布请求时,一切正常。我用 JsonConverter 序列化对象。

现在假设,我想向 MyInfo 添加一个新参数,如下所示

 public class MyInfo 
        {
            [JsonProperty(PropertyName = "n")]
            public int MyInt1{ get; set; }

            [JsonProperty(PropertyName = "v")]
            public string MyString1{ get; set; }

            [JsonProperty(PropertyName = "s")]
            public MyEnum EnumValue{ get; set; }
    }

我更新了网站和 webapi,并发布了我的桌面应用程序的新版本(版本 2)。

webapi 在我的桌面应用程序版本 2 中运行良好。但是,当我使用我的版本 1 时,webapi 中的参数(MyInfo info)以 null 结尾。 提醒一下,版本 1 发送完全相同的 MyInfo 对象,只是缺少一个属性。

如果属性缺失,我该如何避免这个问题并定义一个默认值。

谢谢

【问题讨论】:

    标签: asp.net asp.net-mvc-5 json.net asp.net-web-api


    【解决方案1】:

    事实上,问题在于我有两个具有相同 Json 属性名称的属性。

    它与默认值完全无关。

    【讨论】:

      猜你喜欢
      • 2021-06-10
      • 1970-01-01
      • 2021-09-20
      • 2016-10-27
      • 2014-01-04
      • 2015-06-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多