ASP.Net Core 2 JSON案例转换

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

嗨,我正在努力,甚至在研究了所有那些之后。我正在使用ASP.Net Core 2,并发现在版本1中,驼峰案例的转换是默认的。但是我注意到默认情况下我得到Pascal案例

所以我试着在启动时修复它...

                .AddMvcCore()

            .AddJsonOptions(options =>
            {
                options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                options.SerializerSettings.DefaultValueHandling = DefaultValueHandling.Include;
                options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
            })
            .AddApiExplorer();

但它没有用,我得到Pascal案。如果我在控制器中做同样的事情是有效的,我出于明显的原因我不想这样做。

                var json = JsonConvert.SerializeObject(legalTerms, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver(),
                DefaultValueHandling = DefaultValueHandling.Include,
                NullValueHandling = NullValueHandling.Ignore
            }
            );
            return Ok(json);

有人可以赐教吗?

json asp.net-core
1个回答
1
投票
services
.AddCors(options =>
{
    options.AddPolicy("CorsPolicy",
        builder => builder.AllowAnyOrigin()
            .AllowAnyMethod()
            .AllowAnyHeader()
            .AllowCredentials());
})
.AddAutoMapper(typeof(Startup))
.AddMvcCore()
.AddJsonFormatters() //this does the work
.AddApiExplorer();

Answered here

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