asp.net 仅一种方法的 JsonResult 的核心 PascalCase

asp.net core PascalCase for JsonResult for just one method

使用 MVC 核心。 Json 结果正在发送给修改属性的客户端!它正在将所有内容转换为驼峰命名法。我只想将一种方法更改为 return 数据而不进行修改。 (因为我无法控制 visual studio 中的整个项目,所以我无法更改全局 mvc 设置)目前这搞乱了我的方法并将所有内容都转换为驼峰式:

[HttpPost]
public async Task<JsonResult> CustomerSearch(string search)
{
...
// This is changing the json to camelCase
return Json(lstCustomers);
}

我试图通过使用 return:

将其保留为默认值
var pascalCaseFormatter = new JsonSerializerSettings();
pascalCaseFormatter.ContractResolver = new DefaultContractResolver();
return Json(lstCustomers, pascalCaseFormatter);

然而,这 return 是来自服务器的错误 500。任何帮助将不胜感激。

这不会更改大小写(使用普通 c# 命名约定时的 Pascal 大小写)

return Json(lstCustomers, new JsonSerializerOptions());

这将 return 驼峰式

var serializeOptions = new JsonSerializerOptions
{
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
return Json(lstCustomers, serializeOptions);

最后,OP 正在尝试的将抛出异常:

return Json(lstCustomers, new JsonSerializerSettings());

System.InvalidOperationException: Property 'JsonResult.SerializerSettings' must be an instance of type 'System.Text.Json.JsonSerializerOptions'.

使用 asp.net Core 5 应用进行测试。