【问题标题】:ASP.Net Core 2 JSON case conversionASP.Net Core 2 JSON 大小写转换
【发布时间】:2017-12-17 12:22:38
【问题描述】:

嗨,我有点挣扎,即使在研究了所有内容之后。我正在使用 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】:
    services
    .AddCors(options =>
    {
        options.AddPolicy("CorsPolicy",
            builder => builder.AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowCredentials());
    })
    .AddAutoMapper(typeof(Startup))
    .AddMvcCore()
    .AddJsonFormatters() //this does the work
    .AddApiExplorer();
    

    Answered here

    【讨论】:

      猜你喜欢
      • 2021-11-05
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 2020-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多