Return json with lower case first letter of property names

前端 未结 4 1926
走了就别回头了
走了就别回头了 2021-02-01 17:43

I have LoginModel:

public class LoginModel : IData
{
    public string Email { get; set; }
    public string Password { get; set; }
}

and I hav

4条回答
  •  被撕碎了的回忆
    2021-02-01 18:05

    You can add the two following statement in the configuration of the web API or to the startup file

    using Newtonsoft.Json;
    using Newtonsoft.Json.Serialization;
    
    GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Formatting = Formatting.Indented;
    

    But it is very important to use the return Ok() method instead of return Json() otherwise; this will not work.

    if you have to use Json method (and have no other choice) then see this answer https://stackoverflow.com/a/28960505/4390133

提交回复
热议问题