.NET 8 Httpclient post参数将其大小写更改为camel

问题描述 投票:0回答:1

我在使用名为 client 的 IhttpClientFactory 时遇到了一个奇怪的问题。
“POST”主体参数将其大小写更改为驼峰“initCap”。我编写了一些处理程序(委托处理程序)来添加“http headers”、“异常处理”和“审核日志记录”。
参数正在将大小写更改为驼峰式。

我的模特课

 public class PostRequest
 {
    [DataMember(Name = "Request", EmitDefaultValue = false)]
    public Request? Request { get; set; }
 }

我知道该属性适用于 Newtonsof json,但也尝试过 JsonProperty 但不起作用。 ......

获取 post 调用的响应(在通用 calss 方法中

var httpContent = await client.PostAsJsonAsync(resource, requestBody, cancellationToken);
return await httpContent.Content.ReadFromJsonAsync<T>(cancellationToken: cancellationToken);

有人遇到类似问题吗?

c# asp.net-core dotnet-httpclient .net-8.0
1个回答
1
投票

PostAsJsonAsync
使用
JsonSerializerDefaults.Web
作为序列化器选项,这将导致驼峰式大小写属性名称和
DataMemeberAttribute
不支持数据契约(
System.Text.Json
),因此如果您想使用另一组设置进行序列化 -你可以提供它们。例如:

var httpContent = await client.PostAsJsonAsync( resource, requestBody, new JsonSerializerOptions(), cancellationToken);
或使用 

JsonPropertyNameAttribute


    

© www.soinside.com 2019 - 2024. All rights reserved.