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()?