12

In asp.net core 2.2 I used to have the following,

  var jsonSettings = new JsonSerializerSettings
  {
    ContractResolver = new SubstituteNullWithEmptyStringContractResolver()
  };

services.AddMvc(options =>
{
        options.OutputFormatters.RemoveType<JsonOutputFormatter>();
        options.OutputFormatters.Add(new ResponseJsonOutputFormatter(jsonSettings,ArrayPool<char>.Shared));
}

public class ResponseJsonOutputFormatter : JsonOutputFormatter
{
 // Stuff in here
}

However in 3.0 using:

services.AddControllersWithViews(options =>

and the type JsonOutputFormatter is no longer available.

What is the current suggested way of customizing a json response globally?

I tried using IOutputFormatter but it doesn't seem to be wired in when I set it within AddControllersWithViews as an OutputFormatters so not sure if there are extra steps?

Would middleware with the new endpoint routing be an option? Or is there a better way of achieving this?

jazb
  • 5,498
  • 6
  • 37
  • 44
Michael Esteves
  • 1,445
  • 3
  • 21
  • 38
  • Related: [Where did IMvcBuilder AddJsonOptions go in .Net Core 3.0?](https://stackoverflow.com/q/55666826/3744182). – dbc Nov 28 '19 at 06:36
  • The up-and-coming replacement to Json.NET, [tag:System.Text.Json], does not currently have a public equivalent to Json.NET's contract resolver. As such, you may want to stick with Json.NET. – dbc Nov 28 '19 at 06:37
  • 2
    alternative is `NewtonsoftJsonOutputFormatter` – tchelidze Aug 31 '20 at 12:39

2 Answers2

11

I personally use Json.NET

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

Giddy Naya
  • 4,237
  • 2
  • 17
  • 30
  • 5
    This doesn't work if what you're doing is rewritting the output into a different structure. It only deals with the options of how the format looks. – James Hancock Jan 21 '20 at 13:10
  • Okay... but, how do I add it *globally* though? -- (i.e. OData isn't going to use this; and there is no extension method called `AddNewtonsoftJson` that I can add to the `services.AddOData()` call.) – BrainSlugs83 Mar 07 '21 at 10:54
2

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.
        });
}
x5657
  • 1,172
  • 2
  • 13
  • 26