Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In an ASP.NET Core Web App, what's the difference between AddJsonOptions and AddJsonFormatters?

I'm trying to control all json output settings across the board, like for normal HTTP 200 OK results to things like when model validation fails (HTTP 400 BAD Request) etc.

I ran across these two methods in startup.cs :-

  • AddJsonOptions(options => ...)
  • AddJsonFormatters(options => ...)

Can someone please explain the difference between these two? Why I would use one over the other etc?

FWIW, I'm also trying to use Newtonsoft JSON as my json provider under the hood, with settings like this:

var settings = new JsonSerializerSettings
{
    ContractResolver = new CamelCasePropertyNamesContractResolver(),
    Formatting = Formatting.None,
    NullValueHandling = NullValueHandling.Ignore,
    DateFormatHandling = DateFormatHandling.IsoDateFormat
};
like image 861
Pure.Krome Avatar asked Nov 25 '17 23:11

Pure.Krome


People also ask

What is AddJsonOptions?

AddJsonOptions. This provides the means to configure the serailization settings and contract resolver which have already setup by calling . AddMvc() on the service collection. You can see from the source code that AddJsonFormatters() is already called by AddMvc() .

What is Microsoft Aspnetcore app?

ASP.NET is a popular web-development framework for building web apps on the . NET platform. ASP.NET Core is the open-source version of ASP.NET, that runs on macOS, Linux, and Windows.


1 Answers

AddJsonOptions

This provides the means to configure the serailization settings and contract resolver which have already setup by calling .AddMvc() on the service collection. You can see from the source code that AddJsonFormatters() is already called by AddMvc(). Hence, JSON.net will be used for serialization with a DefaultContractResolver and default settings provided. However, you can use AddJsonOptions() in this situation to override the defaults and specify your desired contract resolver and serializer settings e.g.

services.AddMvc()
    .AddJsonOptions(o => 
    {
        o.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        o.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
    });

AddJsonFormatters

If you are using the barebones AddMvcCore() on your service collection, then formatting for JSON serialization middleware will not be setup by default (see the source on GitHub repo). Consequently, you will need to call AddJsonFormatters() in order to explicitly setup and configure the resolver and serializer settings:

services.AddMvcCore()
    .AddJsonFormatters(o =>
    {
        o.ContractResolver = new CamelCasePropertyNamesContractResolver();
        o.NullValueHandling = NullValueHandling.Ignore;
    });

Summary

You can see that both of these methods are very similar. The only reason that they both exist is because AddMvcCore() allows you to setup or create your own Serialization middleware. If you like the barebones setup that AddMvcCore() provides but want to use the JSON serialization formatting provided by AddMvc() then just call services.AddMvcCore().AddJsonFormatters().

For a more in-depth description of the differences between AddMvc() and AddMvcCore() see the excellent post on What is the difference between AddMvc() and AddMvcCore()?

like image 162
SpruceMoose Avatar answered Oct 12 '22 22:10

SpruceMoose