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?

3

2 Answers 2

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.

4
  • 4
    This does not work with .net2.1. I tried it earlier.
    – saviour123
    Jun 27, 2018 at 14:33
  • Well, this works fine for me. Try a restore using dotnet restore. And check your references.
    – Raphael
    Jun 28, 2018 at 6: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, 2018 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, 2018 at 11:31
1

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

Microsoft.AspNetCore.Mvc

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.