【问题标题】:Configuring System.Text.Json Camel Case in ASP.Net Core 3.x DI在 ASP.Net Core 3.x DI 中配置 System.Text.Json Camel Case
【发布时间】:2020-11-14 23:42:18
【问题描述】:

我想在 ASP.NET Core 中为 JSON 序列化配置 camlecase。 我找到了this 解决方案。但它只适用于 MVC 而不是 WebApi。

所以我已经测试过了:

services.AddControllers().AddJsonOptions(options => {
    options.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
    options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
        });

它在 WebApi 项目中不起作用。

所以问题是:如何在 ASP.NET Core WebApi 项目中配置Camel Case

【问题讨论】:

    标签: c# asp.net asp.net-core json.net


    【解决方案1】:

    在 Startup.cs 文件中

    public virtual void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc()
            .AddJsonOptions(j =>
            {
                j.SerializerSettings.ContractResolver = new DefaultContractResolver()
                {
                    NamingStrategy = new CamelCaseNamingStrategy()
                };
            });
    }
    

    【讨论】:

    • 这是 Newtonsoft 解决方案,OP 要求提供 System.Text.Json 解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 2020-04-24
    • 2020-06-09
    • 1970-01-01
    • 2019-02-14
    相关资源
    最近更新 更多