【问题标题】:PUT Request Does not Convert Snake Case to Pascal CasePUT 请求不会将 Snake 大小写转换为 Pascal 大小写
【发布时间】:2020-07-24 17:08:01
【问题描述】:

我正在尝试发送 PUT 请求,但由于某种原因我的 API 拒绝转换蛇案例属性

请求如下所示:

mercuryParserResponse 在 C# 中看起来像这样:

    [JsonObject(NamingStrategyType = typeof(SnakeCaseNamingStrategy))]
    public class MercuryParserResponseDto
    {
        public string Author { get; set; }
        public string Content { get; set; }
        public DateTimeOffset DatePublished { get; set; }
        public string Direction { get; set; }
        public string Domain { get; set; }
        public string Excerpt { get; set; }
        public string LeadImageUrl { get; set; }
        public string Url { get; set; }
        [JsonProperty("word_count")]
        public int WordCount { get; set; }
        public string NextPageUrl { get; set; } // 
        public int RenderedPages { get; set; } // 
        public string Title { get; set; }
        [JsonProperty("total_pages")]
        public int TotalPages { get; set; }
    }

还有我的 API 端点

        [HttpPut]
        public async Task<IActionResult> Put(
            [FromBody]ImportArticleDto article,
            CancellationToken cancellationToken = default)
        {
            return null;
        }

article.MercuryParserResponse.WordCount 始终是0(请注意,author 等单字属性是正确的)

为什么?为什么我的 Json 属性在这里不起作用?

【问题讨论】:

  • 我试试你的代码,它的工作没有任何问题。您的 Jsonserializer 还有其他设置吗?

标签: c# json serialization .net-core webapi


【解决方案1】:

来自this answer,使用JsonPropertyName 有效:

[JsonPropertyName("word_count")]

但是我真的不想将它添加到每个属性中

如果有人可以建议如何将此外壳应用于整个班级,我会接受您的回答

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-21
  • 1970-01-01
  • 2011-10-29
  • 2022-01-24
  • 2021-03-30
  • 2016-02-24
相关资源
最近更新 更多