Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net core 1.0 web api use camelcase

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

like image 834
Brivvirs Avatar asked Jul 01 '16 07:07

Brivvirs


2 Answers

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.

like image 75
Brivvirs Avatar answered Oct 18 '22 09:10

Brivvirs


You can configure JSON behavior this way:

public void ConfigureServices(IServiceCollection services)     {       services.AddMvc()                   .AddJsonOptions(options =>                   {                       options.SerializerSettings.ContractResolver =                           new CamelCasePropertyNamesContractResolver();                   });   } 
like image 40
Siavash Rostami Avatar answered Oct 18 '22 07:10

Siavash Rostami