asp.net core 1.0 web api use camelcase
23,954
Solution 1
services
.AddMvc()
.AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver
= new Newtonsoft.Json.Serialization.DefaultContractResolver();
});
This keeps a JSON object's name the same as .NET class property.
Solution 2
You can configure JSON behavior this way:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver();
});
}
Author by
Brivvirs
By day: Full stack asp.net developer By night: ZZZzzzZZzzz...
Updated on May 01, 2020Comments
-
Brivvirs about 4 years
On
RC2
the same code returns json format with camel case. After netcore 1.0 release i started new project and the same code is returning json in lowercase.Tried multiple solutions but none of them were working web-api-serialize-properties-starting-from-lowercase-letter
-
Omu almost 8 yearsthis actually now is the default behavior (unfortunately), he wanted Default, as is, no change in property names, case
-
Kyle Gobel over 7 yearsWhat namespace/references does this require. There is no 'AddJsonOptions' that I'm able to see
-
Brivvirs over 7 yearsIts MVC6.namespace Microsoft.Extensions.DependencyInjection // Extensions methods for configuring MVC via an Microsoft.Extensions.DependencyInjection.IMvcBuilder. class MvcJsonMvcBuilderExtensions
-
JMK over 6 years@Omu These actually aren't the same (in .net core 2.0 anyway). The DefaultContractResolver will accept CamelCase and return TitleCase, the CamelCasePropertyNamesContractResolver accepts and returns CamelCase.
-
Hinrich over 6 years@Omu has this changed from .net core 1.x to 2.0 ?
-
Floxy about 6 yearsthanks !! i have the same Problem this one work fine
-
ibrahimozgon over 5 yearsWorks fine. Thanks.