Turning off camelCasing, PascalCasing in JSON response from ASP.NET Core 3.1 API

171 0

In ASP.NET Core 3.1 api the default json response formatting is camelCase.

When you have a model in this way


public class EmployeeModel
{
[Key]
public int Id { get; set; }
public string FirstName { get; set; }

}

private static readonly List myEmployeeModel = new List
{
new EmployeeModel {Id = 1, FirstName = “David zongo”},
new EmployeeModel {Id = 2, FirstName = “Koudbi”, },
};

The call in a Get
[HttpGet]
public IEnumerable Get()
{
return myEmployeeModel;
}
The result of this call
[
{"id":1, "firstName":"David zongo"},
{"id":2,"firstName":"Koudbi"}
]

As you can see if you want to receive the result with FirstName

Here is the formatting to add under startup.cs:

services.AddControllers()
.AddNewtonsoftJson(options =>
{
options.UseMemberCasing();
});

Related Post