0

I have around 30 entities. I use ajax call to retrieve data from database using entity framework. We have requirement to use the the same column name as in database's table. However ajax get the response and property display in camel case.

public  Class ABC
{
  public string RT_ID { get; set; }
  public string START_DATE { get; set; }
}

the JSON response shows rtId and startDate corresponding for RT_ID and START_DATE .

However JSON response show RT_ID and START_DATE, I use JsonPropertyName attribute against each property.

public  Class ABC
{
 [JsonPropertyName("RT_ID")]
  public string RT_ID { get; set; }
 [JsonPropertyName("START_DATE")]
  public string START_DATE { get; set; }
}

So the challenge is, Can I have same property name in JSON response without adding JsonPropertyName to each property of the class. May be at class level ?

Thanks

PowerTech
  • 230
  • 3
  • 14
  • Better to use Newtonsoft.Json, you will not have these problems – Serge Mar 02 '22 at 20:55
  • This is typically caused by your JSON serialization provider being either configured with, or defaulting to a particular naming strategy such as camelCase. These often have options to respect or override specified names (`[JsonPropertyName]`) For instance if Newtonsoft's JSON serialization is used, specifying the `DefaultNamingStrategy` should preserve your property-names as they are defined in the class. – Steve Py Mar 02 '22 at 21:32

1 Answers1

1

Try using below code:

Starup.cs

services.AddControllers().AddJsonOptions(jsonOptions =>
{
    jsonOptions.JsonSerializerOptions.PropertyNamingPolicy = null;
})

In Asp.net Core 3 by default PropertyNamingPolicy is camelCase and set it to null will solve your problem.

For more details view this thread ASP NET Dataset Column name format

saif
  • 26
  • 1