Search code examples
asp.net-core.net-core-3.1

The type or namespace name 'MvcJsonOptions' could not be found in .net core 3.1


I have converted my .net core 2.2 to .net core 3.1. I am getting issue with following below.

Getting complain about MvcJsonOptions inside IOptions<MvcJsonOptions> options.

Also getting issue with MvcOptions inside IOptions<MvcOptions>.

Another one is, options.SerializerSettings inside SetupSerialiserSettings(options.SerializerSettings).

        public JsonDeserialiser(IOptions<MvcJsonOptions> options) : this(options.Value.SerializerSettings)
        {

        }

           services.AddSingleton<IObjectModelValidator>(
            s =>
            {
                var options = s.GetRequiredService<IOptions<MvcOptions>>().Value;
                
            });
           services.AddControllers()
                .AddJsonOptions(options => SetupSerialiserSettings(options.SerializerSettings))

Solution

  • Firstly,you can refer to the link and find MvcJsonOptions only applies to 2.1, 1.0, 1.1, 2.0, 2.2.And you can also refer to Breaking changes to Microsoft.AspNetCore.App in 3.0.

    In .net core 3.1,the internal usage of Json.NET in ASP.NET Core has be replaced by the new platform-provided JSON APIs.Refer to The future of JSON in .NET Core 3.0.

    So you can try to add package Microsoft.AspNetCore.Mvc.NewtonsoftJson,and use like the following code(from the official doc):

    services.AddControllers().AddNewtonsoftJson(options =>
    {
        ...
    });