Serialization In Web API With ASP.NET Core

The Web API serializes all the public properties into JSON. In the older versions of Web API, the default serialization property was in PascalCase. When we are working with .NET based applications, the casing doesn’t matter. But when the API is consumed by another application, such as Angular JS or any other application, then most of the cases are CamelCase as per the JavaScript standard. Moreover, most of the developers are familiar with this type of naming.
 
ASP.NET Core becomes the camelCase serialization as default. If we have migrated the application from Web API 2.0 to .NET core, the application may not be working. However, we can configure the serialization to PascalCase.
 
Product.cs
  1. public class Product  
  2.     {  
  3.         public int Id { getset; }  
  4.   
  5.         public string Name { getset; }  
  6.   
  7.         public string Description { getset; }  
  8.   
  9.     }  
ProductController.cs
  1. [Route("api/[controller]")]  
  2.    public class ProductController : Controller  
  3.    {              
  4.        [HttpGet("{id}")]  
  5.        public Product Get(int id)  
  6.        {  
  7.            return new Product()  
  8.            {  
  9.                Id=id,  
  10.                Name="test",  
  11.                Description="Description"  
  12.            };  
  13.        }  
  14.         
  15.    }  
Startup.cs
  1. public class Startup  
  2.    {        
  3.        public void ConfigureServices(IServiceCollection services)  
  4.        {  
  5.            services.AddMvc();  
  6.              //  .AddJsonOptions(o => o.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver());  
  7.        }  
  8.   
  9.        
  10.        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)  
  11.        {             
  12.            app.UseMvc();                 
  13.             
  14.        }  
  15.    }  
While executing the API service, the result will be as follows.
  1. {"id":1,"name":"test","description":"Description"}  
To configure the Service to PascalCase, we need to change the ContractResolver for the JSON Serializer (DefaultContractResolver). 
 
Startup.cs
  1. public class Startup  
  2.    {        
  3.        public void ConfigureServices(IServiceCollection services)  
  4.        {  
  5.            services.AddMvc()  
  6.                        .AddJsonOptions(o => o.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver());  
  7.        }  
  8.   
  9.        
  10.        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)  
  11.        {             
  12.            app.UseMvc();                 
  13.             
  14.        }  
  15.    }  
  1. {"Id":1,"Name":"test","Description":"Description"}  
Ebook Download
View all
Learn
View all