I have a strange issue using IhttpClientFactory named client.
The "POST" body parameter are changing it's case to camel "initCap". I have written some Handlers (delegate handler) for adding "http headers", "exception handling" and "audit logging".
The parameters are changing the case to camelcase.

我的模特类

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

我知道这个属性是用于Newtonsof json的,但也try 了JsonProperty,但没有工作. .....

为了获得post调用的响应(在一个通用的calss方法中,

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

有没有人遇到过类似的问题?

推荐答案

默认情况下,PostAsJsonAsync使用JsonSerializerDefaults.Web作为序列化选项,这将导致Camel大小写属性名和data contracts (DataMemeberAttribute) are not supported by System.Text.Json.如果您想使用另一组序列化设置(即Pascal大小写默认序列化)—您可以提供它们.例如:

var httpContent = await client.PostAsJsonAsync(
    resource, 
    requestBody,
    new JsonSerializerOptions(), // TODO: configure as needed and reuse the options
    cancellationToken);

或者使用JsonPropertyNameAttribute(将需要标记所有需要"非默认"处理的属性).例如:

public class PostRequest
{
    [JsonPropertyName("Request")] // should be Pascal case no matter which System.Text.Json settings used
    public Request? Request { get; set; } 
}

另见:

Csharp相关问答推荐

需要更改哪些内容才能修复被覆盖的财产中的无效警告CS 8765?

AutoMapper -如何为两个不同的用例设置单个映射?

是否可以使用EF—Core进行临时部分更新?

从应用程序图API调用访问所有者字段

不带身份的Blazor服务器.Net 8 Cookie身份验证

自动映射程序在GroupBy之后使用项目

在C#中,有没有一种方法可以集中定义跨多个方法使用的XML参数描述符?

Take()方法如何与IAsyncEnumerable一起使用

BlockingCollection T引发意外InvalidOperationException

.NET并发词典交换值

无法将生产环境的AppDbContext设置替换为用于集成测试的内存数据库

我可以查看我们向应用程序洞察发送了多少数据吗?

无法使用[FromForm]发送带有图像和JSON的多部分请求

使用switch 类型模式时出现奇怪的编译器行为

在扩展方法中,IEnumerable<;T>;不会转换为IEumerable<;T&>

JsonPath在Newtonsoft.Json';S实现中的赋值

.NET8Blazor-为什么Rapzor渲染在for循环之后显示?

C# Winforms:从对象树到TreeView的递归转换重复条目

RCL在毛伊岛应用程序和Blazor服务器应用程序.Net 8.0中使用页面

Xamarin.Forms-如何创建可 Select 的显示alert 或弹出窗口?