How to use default Newtonsoft.Json in Asp.net Core Web Api?

I am new to ASP.Net Web Api Core . I have been using ASP.Net MVC for the past few years, and I have always written ActionFilter and used JSON.Net for Serializing data in JSON . So I replaced Microsoft's JavaScript Serializer (which is slower than JSON.Net ) with JSON.Net (allegedly 400% faster).

How to do all this in ASP.Net Web Api Core ? Where to change the default formattor ?

Note. Please feel free to ask if you have any questions.

thanks

+18
json serialization asp.net-web-api asp.net-core
source share
2 answers

ASP.NET Core already uses JSON.NET, as JavaScriptSerializer not implemented / ported to .NET Core.

Microsoft.AspNetCore.Mvc depends on Microsoft.AspNetCore.Formatter.Json which depends on Microsoft.AspNetCore.JsonPatch , which depends on Newtonsoft.Json (see Source ).

Refresh

This is true only for ASP.NET Core 1.0 through 2.2. ASP.NET Core 3.0 eliminates JSON.NET dependency and uses its own JSON serializer.

+26
source share

Here is the code snippet for setting the parameters of the main .net application

 public void ConfigureServices(IServiceCollection services) { services .AddMvc() .AddJsonOptions(options => { // send back a ISO date var settings = options.SerializerSettings; settings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat; // dont mess with case of properties var resolver = options.SerializerSettings.ContractResolver as DefaultContractResolver; resolver.NamingStrategy = null; }); } 
+5
source share

All Articles