14

Previously with .net 2.0, you can add json stuffs this way

 services.AddJsonOptions(options => {
                    options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                    options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            });

or

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

I realize I can't do the same thing with .net 2.1.

I get this error:

'IServiceCollection' does not contain a definition for 'AddJsonOptions'
and the best extension method overload 'MvcJsonMvcBuilderExtensions.AddJsonOptions(IMvcBuilder, Action<MvcJsonOptions>)' 
requires a receiver of type 'IMvcBuilder

Anyone have a solution?

saviour123
  • 1,095
  • 2
  • 13
  • 21

2 Answers2

20

Use

services.AddMvc().AddJsonOptions(...)

to configure it.

Above extension method can be found in Microsoft.AspNetCore.Mvc.Formatters.Json Version 2.1.0.0. Either include this package directly, or add one of these two Microsoft.AspNetCore.App / Microsoft.AspNetCore.All.

Raphael
  • 990
  • 1
  • 13
  • 24
  • 4
    This does not work with .net2.1. I tried it earlier. – saviour123 Jun 27 '18 at 14:33
  • Well, this works fine for me. Try a restore using `dotnet restore`. And check your references. – Raphael Jun 28 '18 at 06:19
  • Okay-- So I have observed that the default mvc was overridden. Now. I've observed that `.AddJsonOptions(...)` with *`ReferenceLoopHandling = ReferenceLoopHandling.Ignore` added. However, it seems not to have effect since my the code keep throwing ` "Message": "Self referencing loop detected with type 'Lemon.Logic.Models.Customers.Customer'. Path 'Client.Customers[0].Person.Customers'.", "Source": "Newtonsoft.Json`* – saviour123 Jun 28 '18 at 10:47
  • Make sure you actually use your customized options. But since i do not know more about your code i cannot help you further. – Raphael Jun 28 '18 at 11:31
1

Found the problem - you need to make sure you've got a reference to

Microsoft.AspNetCore.Mvc

David McEleney
  • 3,397
  • 1
  • 26
  • 32