Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

services AddJsonOptions .net core 2.1

Tags:

.net

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?

like image 427
saviour123 Avatar asked Jun 27 '18 13:06

saviour123


2 Answers

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.

like image 129
Raphael Avatar answered Sep 18 '22 14:09

Raphael


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

Microsoft.AspNetCore.Mvc

like image 38
David McEleney Avatar answered Sep 20 '22 14:09

David McEleney