ASP.Net Core 2 JSON案例转换

时间:2017-11-01 19:44:13

标签: json asp.net-core

嗨,我正在努力,甚至在研究了所有那些之后。我正在使用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);

有人可以赐教我吗?

1 个答案:

答案 0 :(得分: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