IMvcBuilder.AddJsonOptions method deprecation in .NET Core 3.0 and later
After upgrading your ASP.NET Web API project from .NET Core 2.0 to 3.0, you may encounter the error "'IMvcBuilder' does not contain a definition for 'AddJsonOptions'". This is because the AddJsonOptions extension method in the Microsoft.AspNetCore.Mvc.Formatters.Json package has been deprecated.
In .NET Core 3.0, the ASP.NET Core team no longer includes Json.NET by default. The new JSON API introduced provides improved performance. However, Json.NET can be reimplemented in ASP.NET Core 3.0 projects.
To reconfigure your project to use Json.NET, add the NuGet package Microsoft.AspNetCore.Mvc.NewtonsoftJson. Then, in your Startup's ConfigureServices method, configure MVC using the following steps:
<code class="language-csharp">services.AddControllers().AddNewtonsoftJson();</code>
You can also use overloads of the AddNewtonsoftJson method to configure Json.NET options, just like you use AddJsonOptions in ASP.NET Core 2.x.
<code class="language-csharp">services.AddControllers().AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());</code>
By following these steps, you can retrieve the functionality previously provided by the AddJsonOptions method in .NET Core 3.0 while incorporating the performance enhancements of the new JSON API.
The above is the detailed content of How to Replace IMvcBuilder.AddJsonOptions in .NET Core 3.0 and Beyond?. For more information, please follow other related articles on the PHP Chinese website!