asp.net core webapi 返回Json属性名全部小写怎么设置
比如:
public class User
{
public string Name { get; set; }
public int Age { get; set; }
public string Address_Info { get; set; }
}
返回:Address_Info 为 address_Info 怎么能是 address_info
services.AddMvc() .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new LowercaseContractResolver());
public class LowercaseContractResolver : Newtonsoft.Json.Serialization.DefaultContractResolver { protected override string ResolvePropertyName(string propertyName) { return propertyName.ToLower(); } }
终于搞定了这个大小写的问题,之前全在AddMvc中进行操作,今天才知道直接用AddJsonOptions中设置生效。Thank You!!!
找半天方法,终于看到了
json.net的配置
的之前的webapi中会设置,在.net core webapi 中怎么设置
看看你用的json序列化的配置吧。
参考如下,增加JsonProperty属性设置:
/// <summary> /// 错误码 /// </summary> [XmlElement("errorCode")] [JsonProperty("errorCode")] public string ErrorCode { get; set; }