JsonOutputFormatter in ASP.NET Core 3.0
To revert back to NewtonsoftJson and also configure its output formatter, first add a package reference to Microsoft.AspNetCore.Mvc.NewtonsoftJson
and then in ConfigureServices you need to call Configure after calling AddController
and AddNewtonsoftJson
:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers()
.AddNewtonsoftJson();
services.Configure<MvcOptions>(options =>
{
NewtonsoftJsonOutputFormatter jsonOutputFormatter = options.OutputFormatters.OfType<NewtonsoftJsonOutputFormatter>().Single();
// makes changes to the Newtonsoft JSON Output Formatter here.
});
}
I personally use Json.NET
- Simply add a package reference to Microsoft.AspNetCore.Mvc.NewtonsoftJson.
- Update Startup.ConfigureServices to call AddNewtonsoftJson.
services.AddMvc().AddNewtonsoftJson();
Json.NET settings can be set in the call to AddNewtonsoftJson
:
services.AddMvc()
.AddNewtonsoftJson(options =>
options.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver());
I am using the default options with compatibility mode
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
.AddNewtonsoftJson(options => { options.SerializerSettings.ContractResolver =
new DefaultContractResolver(); });
Reference Migrate from ASP.Net 2.2 to 3.0