11

I'm migrating my ASP.NET 1.1 project to 2.0:

Inside the Setup class, under the Configure method override I have:

services.AddMvc()
    .AddJsonOptions(options =>
        options.SerializerSettings.Converters.Add(new StringEnumConverter())
    );

The AddJsonOptions method is missing.

What happened to it? How can I get the same functionality?

SuperJMN
  • 13,110
  • 16
  • 86
  • 185
  • It doesn't seem to have gone anywhere - It works in my ASP.NET Core 2.0 project. It's defined in [`MvcJsonMvcBuilderExtensions`](https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.Formatters.Json/DependencyInjection/MvcJsonMvcBuilderExtensions.cs). – Kirk Larkin Sep 05 '17 at 11:58
  • 1
    it's still there; make sure you're using the right packages and ns, `ctrl + .` is your friend – The Bearded Llama Sep 05 '17 at 12:21
  • Had to still separately add `using Newtonsoft.Json.Serialization;` depending what you want to do next. In my case I wanted `AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());` but `DefaultContractResolver` is not recognized. Really annoying that you have to magically intuit the requirement for all these damn things in .NET. – mc01 May 10 '19 at 22:11

1 Answers1

15

AddJsonOptions is defined in Microsoft.AspNetCore.Mvc.Formatters.Json nuget package.

If your project has a dependency to Microsoft.AspNetCore.All metapackage (in your .csproj: <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0"/> ) then you already have it. Otherwise, you may need to add that package directly.

After that do:

  • dotnet restore
  • check that you have using Microsoft.Extensions.DependencyInjection;
Kirk Larkin
  • 84,915
  • 16
  • 214
  • 203
Set
  • 47,577
  • 22
  • 132
  • 150