Home >Backend Development >C++ >Why is `IMvcBuilder.AddJsonOptions` Missing in ASP.NET Core 3.0 and How Can I Configure JSON Serialization?

Why is `IMvcBuilder.AddJsonOptions` Missing in ASP.NET Core 3.0 and How Can I Configure JSON Serialization?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-23 01:36:09547browse

Why is `IMvcBuilder.AddJsonOptions` Missing in ASP.NET Core 3.0 and How Can I Configure JSON Serialization?

The IMvcBuilder.AddJsonOptions method is missing and the JSON serialization configuration solution in ASP.NET Core 3.0

Problem Description

After upgrading the ASP.NET Web API project from .NET Core 2.0 to 3.0, using IMvcBuilder.AddJsonOptions will report an error, prompting that IMvcBuilder no longer contains this extension method.

Solution

Adopt new JSON API

ASP.NET Core 3.0 no longer uses Json.NET by default in favor of the new high-performance JSON API.

Reconfigure Json.NET (optional)

For compatibility with older projects, you can reconfigure your project to use Json.NET:

  1. Install the Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet package.
  2. Add the following code in the Startup.cs method of the ConfigureServices file:
<code class="language-csharp">services.AddControllers()
    .AddNewtonsoftJson();</code>
  1. Configure Json.NET options:
<code class="language-csharp">services.AddControllers()
    .AddNewtonsoftJson(options =>
    {
        options.SerializerSettings.ContractResolver = new DefaultContractResolver();
    });</code>

The above is the detailed content of Why is `IMvcBuilder.AddJsonOptions` Missing in ASP.NET Core 3.0 and How Can I Configure JSON Serialization?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn