71

I am new to ASP.Net Web Api Core. I have been using ASP.Net MVC for past few years and I always have written an ActionFilter and used JSON.Net for Serializing data into JSON. So, in that way I replaced Microsoft's JavaScript Serializer (which is slower than JSON.Net) with JSON.Net (it is claimed to be 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

0

3 Answers 3

100

In .NET Core 3.0+ include the NuGet package Microsoft.AspNetCore.Mvc.NewtonsoftJson and then replace

services.AddControllers();

in ConfigureServices with

services.AddControllers().AddNewtonsoftJson();

This is a pre-release NuGet package in .NET Core 3.0 but a full release package in .NET Core 3.1.

I came across this myself, but I've found that the same answer with some additional info is in this SO question and answer.

Edit: As a useful update: code with the call to AddNewtonsoftJson() will compile and run even without installing the Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet package. If you do that, it runs with both converters installed, but defaulting to the System.Text.Json converter which you presumably don't want since you're reading this answer. So you must remember to install the NuGet package for this to work properly (and remember to re-install it if you ever clear down and redo your NuGet dependencies).

Sign up to request clarification or add additional context in comments.

4 Comments

this saved my lot of time, and i was able to migrate to .net 3.1. seamlessly
This saves me! Thanks. I upgraded my application from 2.2 to 6. There are lots of cases that uses dynamics object. So instead of resolving them on every case, I tried this solution and it works like a charm. Thank you. services.AddControllers().AddNewtonsoftJson();
I did same, but this is breaking my JsonConvert.DeserializeObject. Below is what I did after getting response from controller: var res = response.Content.ReadAsStringAsync().Result; var validationResponse = JsonConvert.DeserializeObject<myModelName>(res); Its breaking at the desterilize line. If I use services.AddControllers(); only, post will work, but patch wont.
Is this information outdated? It doesn't seem to work, and the mentioned package doesn't exist. EDIT: Package exists. It was just Nuget package manager being slow as hell as always.
36

ASP.NET Core already uses JSON.NET as JavaScriptSerializer isn't 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).

Update

This is only true for ASP.NET Core 1.0 to 2.2. ASP.NET Core 3.0 removes the dependency on JSON.NET and uses it's own JSON serializer.

8 Comments

Hi Tseng. Thanks for taking time and helping. I have commented out the newtonsoft's dependency in project.json but web api is still working, how is that?
It told you already. You are referencing one (likely `Microsoft.AspNetCore.Mvc´) and it references Formatter.Json and it has dependency on JsonPatch and JsonPatch finally brings in Newtonsoft.Json. You can't "comment out" the JSON.NET dependency unless you remove all Mvc related packages, but then you got a console application
So basically, even if I remove the reference, it still loads it because JSON.Net is a dependency for JsonPatch? Got it. Thanks.
@SuperCoder: Exactly. It will however load the version which is referenced by MVC (9.0.1 as of ASP.NET Core MVC 1.1). If you want a newer version (i.e. Json.NET 9.0.2-beta2 for example), then you need to add the reference yourself to your application csproj/project.json
Great info... I discovered the version used by the .Net Core MVC is Newtonsoft.Json v9.0.1
|
6

here is a code snippet to adjust the settings for a .net core 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;
        });
}

2 Comments

This code is for setting the default configurations of the Newtonsoft.json not for changing the serializer.
so this works, but i need special cases, i.e. sometime i might just need yyyy-MM-dd. I've got a custom attribute for this, [JsonConverter(typeof(DateOnlyConverter))] that works with regular newtonsoft, how do i make Microsoft.AspNetCore.Mvc.NewtonsoftJson recognize my custom attribute?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.