I have a web service with .NET 7. When I get the data from the database and send it as JSON, the first letter of the class items is sent in lowercase.
In order to explain the problem, I wrote a test web service
testClass.cs
public class testClass
{
public String Date1 { get; set; }
public String Date2 { get; set; }
public testClass(string date1, string date2)
{
Date1 = date1;
Date2 = date2;
}
}
and my api:
[HttpGet(Name = "test")]
public testClass Get()
{
return (new testClass("d1", "d2"));
}
the result is:
{
"date1": "d1",
"date2": "d2"
}
But I want the result to be as follows:
{
"Date1": "d1",
"Date2": "d2"
}
2
Answers
Default mode for
JsonSerializerOptions
iscamel-casing
(The first word starts with a small letter and capital letter appears at the start of the second word and at each new subsequent word that follows it).You can get more information in this link:
JsonSerializerOptions.PropertyNamingPolicy
You just need to add the following configuration to
Program.cs
:Or just add JsonProperty: