0

i have this controller method that return an array of objects

 public async Task<ActionResult<List<AllClientsDataModelDb>>> GetAll() 
    {
        var  ReturnValue = new List<AllClientsDataModelDb>();
        ReturnValue = await Clda.GetClients(new { cm = 1 });
        return (ReturnValue);
    }

here is the code of AllClientsDataModelDb class

  public class AllClientsDataModelDb
    {
        public long IDCLIENT { get; set; }
        public string CL_CODE { get; set; }
        public string CL_NOM { get; set; }
        public string CL_ADRESSE { get; set; }
        public string CL_CODEPOS { get; set; }
        public string CL_VILLE { get; set; } = null;
        public int CL_ETATCOMPTE { get; set; }
        public int CL_AlerteCompta { get; set; }
    }

but the result of that method (in browser) does not respect the case sensitivity of the class properties Example :

[{"idclient":1,"cL_CODE":"1","cL_NOM":"EUROPEQUIPEMENTMysql","cL_ADRESSE":"ModifSoft","cL_CODEPOS":"44","cL_VILLE":"STDENIS","cL_ETATCOMPTE":1,"cL_AlerteCompta":0},

{"idclient":2,"cL_CODE":"2","cL_NOM":"A UTOMATISMES-SERVICESzzzz","cL_ADRESSE":null,"cL_CODEPOS":"97420","cL_VILLE":"LEPORT","cL_ETATCOMPTE":1,"cL_AlerteCompta":0},

what i'm doing wrong ?

lzaek
  • 200
  • 1
  • 10
  • 1
    You're not really doing anything wrong. The default serializer settings lowercase the first letter to keep with JSON conventions. You _can_ override the default, see the second answer here: https://stackoverflow.com/a/62154562/1030169 – jmoerdyk Sep 08 '21 at 15:44
  • @jmoerdyk no it does not , look at IDCLIENT for example its lower everything not only the first letter – lzaek Sep 08 '21 at 15:52
  • @jmoerdyk and i'm using Newtonsoft.Json – lzaek Sep 08 '21 at 15:54
  • @jmoerdyk it didn't work – lzaek Sep 08 '21 at 16:02
  • 1
    Please show your attempt at overriding the JSON naming convention. – mason Sep 08 '21 at 18:57
  • 1
    Also, you said in the title that you're using ASP.NET Core, but your question tags the old ASP.NET and ASP.NET Web API frameworks. What actual framework are you using? – mason Sep 08 '21 at 19:16

1 Answers1

0

You need to create your own Json Profile Formatter by inheriting from JsonOutputFormatter.

public class PascalCaseJsonProfileFormatter : JsonOutputFormatter
{
    public PascalCaseJsonProfileFormatter() : base(new JsonSerializerSettings { ContractResolver = new DefaultContractResolver() }, ArrayPool<char>.Shared)
    {
        SupportedMediaTypes.Clear();
        SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse
     ("application/json;profile=\"https://en.wikipedia.org/wiki/PascalCase\""));
    }
}

Then modify your Startup.cs file's ConfigureServices Method like this.

services.AddMvc()
    .AddMvcOptions(options =>
    {
        options.OutputFormatters.Add(new PascalCaseJsonProfileFormatter());
    });

Try this, it should work.

Bluemarble
  • 1,925
  • 4
  • 20
  • 35