首頁 >後端開發 >C++ >ASP.NET Core 3.0:「AddJsonOptions」去哪了?

ASP.NET Core 3.0:「AddJsonOptions」去哪了?

DDD
DDD原創
2025-01-23 01:27:10432瀏覽

ASP.NET Core 3.0: Where Did `AddJsonOptions` Go?

ASP.NET Core 3.0 中 AddJsonOptions 方法的變動

問題:升級到 ASP.NET Core 3.0 後,AddJsonOptions 方法消失了,重新安裝相依性也無法解決問題。

解答:

背景:

ASP.NET Core 3.0 改變了預設的 JSON 序列化機制。 Json.NET 已被一個新的、注重效能的 JSON API 所取代。

使用 Json.NET:

如果您需要 Json.NET 的相容性,請按以下步驟操作:

  1. 安裝 Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet 套件。
  2. Startup 類別的 ConfigureServices 方法中,使用以下程式碼配置 MVC:
<code class="language-csharp">services.AddControllers()
    .AddNewtonsoftJson();</code>

設定選項:

您可以使用重載方法進一步配置 Json.NET 選項:

<code class="language-csharp">services.AddControllers()
    .AddNewtonsoftJson(options =>
    {
        options.SerializerSettings.ContractResolver = new DefaultContractResolver();
    });</code>

以上是ASP.NET Core 3.0:「AddJsonOptions」去哪了?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn