为C#对象上的属性名称关闭CamelCase

时间:2019-02-08 07:11:05

标签: swagger-ui

这是DTO C#类:

public class WeatherForecast
        {
            public string DateFormatted { get; set; }
        }

这是招摇定义中生成的:

{"WeatherForecast":{"type":"object","properties":{"dateFormatted":{"type":"string"}}}}

问题是当我生成XML示例时:

<?xml version="1.0" encoding="UTF-8"?>
<WeatherForecast>
    <dateFormatted>string</dateFormatted>
</WeatherForecast>

问题是,当我将该XML发送回API时,它没有填充DateFormatted属性。如果我更改大小写,使其不再是CamelCase(DateFormatted),它将正常工作,并且该属性将填充“字符串”。

生成摇摇欲坠的定义时如何关闭CamelCase?

1 个答案:

答案 0 :(得分:1)

默认情况下,在aspnet中,骆驼属性名称如此,因此您将Startup更改为

services.AddMvc();

services
        .AddMvc()
        .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());

参考:aspnet