ChatGPT解决这个技术问题 Extra ChatGPT

Where did IMvcBuilder AddJsonOptions go in .Net Core 3.0?

I've just upgraded my ASP web API project from .Net core 2.0 to 3.0. I was using

     services.AddMvc()
             .AddJsonOptions(options =>options.SerializerSettings.ContractResolver 
                                       = new DefaultContractResolver());

previously to ensure lower-casing of the serialized JSON.

After the upgrade to 3.0 I get this error:

Error CS1061 'IMvcBuilder' does not contain a definition for 'AddJsonOptions' and no accessible extension method 'AddJsonOptions' accepting a first argument of type 'IMvcBuilder' could be found (are you missing a using directive or an assembly reference?)

According to AddJsonOptions for MvcJsonOptions in Asp.Net Core 2.2 the AddJsonOptions extension method is/was provided by the Microsoft.AspNetCore.Mvc.Formatters.Json nuget package. I have tried installing/reinstalling this but still can't resolve the method. Interestingly, intellisense only shows Microsoft.AspNetCore.Mvc.Formatters.Xml when I try to add the using statement even though I added the Json nuget package.

Any ideas what is going on? The documentation for AddJsonOptions only goes up to .Net 2.2 so perhaps the method has been deprecated in 3.0 in favor of some other configuration mechanism?


p
poke

As part of ASP.NET Core 3.0, the team moved away from including Json.NET by default. You can read more about that in general in the announcement on breaking changes to Microsoft.AspNetCore.App.

Instead of Json.NET, ASP.NET Core 3.0 and .NET Core 3.0 include a different JSON API that focuses a bit more on performance. You can learn about that more in the announcement about “The future of JSON in .NET Core 3.0”.

The new templates for ASP.NET Core will no longer bundle with Json.NET but you can easily reconfigure the project to use it instead of the new JSON library. This is important for both compatibility with older projects and also because the new library is not supposed to be a full replacement, so you won't see the full feature set there.

In order to reconfigure your ASP.NET Core 3.0 project with Json.NET, you will need to add a NuGet reference to Microsoft.AspNetCore.Mvc.NewtonsoftJson, which is the package that includes all the necessary bits. Then, in the Startup’s ConfigureServices, you will need to configure MVC like this:

services.AddControllers()
    .AddNewtonsoftJson();

This sets up MVC controllers and configures it to use Json.NET instead of that new API. Instead of controllers, you can also use a different MVC overload (e.g. for controllers with views, or Razor pages). That AddNewtonsoftJson method has an overload that allows you to configure the Json.NET options like you were used to with AddJsonOptions in ASP.NET Core 2.x.

services.AddControllers()
    .AddNewtonsoftJson(options =>
    {
        options.SerializerSettings.ContractResolver = new DefaultContractResolver();
    });

For those following along at home... when migrating a project you may need to add a nuget reference to "Microsoft.AspNetCore.Mvc.NewtonsoftJson" to get the "AddNewtonsoftJson" extension. At the time of writing this requires "include prereleases" to be checked if you are using the Visual Studio Nuget manager.
Note that I would generally not recommend migrating to ASP.NET Core 3 with real applications just yet. If you just want to play around, sure, but there are still a few moving bits before the final release, so you will have to keep that in mind if you want to migrate to it early.
For me AddNewtonsoftJson is missing, even after referencing that Microsoft.AspNetCore.Mvc.NewtonsoftJson reference.
@avenmore The new serializer uses a separate set of attributes. You can use [JsonIgnore] from the System.Text.Json.Serialization namespace.
@Louis If you are using ASP.NET Core 3.1, you will need to add version 3.1.x of that package. If you are using ASP.NET Core 3.0 (which isn’t supported any more), you will need to add version 3.0.x of that package.
N
Nicke Manarin

This worked for me, while using .Net Core 3:

services.AddMvc().AddJsonOptions(o =>
{
    o.JsonSerializerOptions.PropertyNamingPolicy = null;
    o.JsonSerializerOptions.DictionaryKeyPolicy = null;
});

I would assume the line should be: o.JsonSerializerOptions.PropertyNameCaseInsensitive = false;
Yeah me too, but it does not. This answer worked for me though.
No need for o.JsonSerializerOptions.DictionaryKeyPolicy = null; and o.JsonSerializerOptions.PropertyNameCaseInsensitive = false;
I wish I could use this answer, but my application was built using Newtonsoft attributes, which the new MS serializer ignores, meaning that I have to continue using the Newtonsoft serializer with AddNewtonsoftJson. Maybe MS could add an option to respect Newtonsoft attributes, while still getting the performance gains they are promising.
@eric Yes you are right but, Newtonsoft performance is enough for most of the situations.
D
Drilon Ahmetaj

Make sure that you installed the Microsoft.AspNetCore.Mvc.NewtonsoftJson package.

https://i.stack.imgur.com/ZW3LF.png


J
Joey

This would help

public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers().AddJsonOptions(options=> {  options.JsonSerializerOptions.PropertyNamingPolicy = null;
                 options.JsonSerializerOptions.DictionaryKeyPolicy = null;

            });

            services.AddDbContext<PaymentDetailContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DevConnection")));
        }

A
Adil Bhatti

This would help try Installing the Nuget Package

Microsoft.AspNetCore.Mvc.NewtonsoftJson


S
SAM

It's work for me, Install the NewtonsoftJson package from NuGet "dotnet add package Microsoft.AspNetCore.Mvc.NewtonsoftJson --version 3.1.0" version 3.1.0 working for ASP.NET Core 3.0 and use the Following Code-

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
    .AddNewtonsoftJson(opt => {
        opt.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
    });

Hope it's Working Fine, Thanks.


J
Justus kasyoki

This worked for me, while using .Net Core 3: click here


Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review