且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从 .NET Core 2.2 迁移到 3.0-preview-9 后,模型绑定停止工作

更新时间:2023-09-17 11:41:22

ASP.NET Core 3 使用 System.Text.Json 而不是 Newtonsoft.Json(又名 JSON).NET) 来处理 JSON.JSON.NET 支持从字符串解析为十进制,但 System.Text.Json 不支持.如文档:

ASP.NET Core 3 uses System.Text.Json instead of Newtonsoft.Json (aka JSON.NET) for handling JSON. JSON.NET supports parsing from a string into a decimal, but System.Text.Json does not. The easiest thing to do at this stage is to switch back to using JSON.NET, as described in the docs:

  • Add a package reference to Microsoft.AspNetCore.Mvc.NewtonsoftJson.
  • Update Startup.ConfigureServices to call AddNewtonsoftJson.

services.AddMvc()
      .AddNewtonsoftJson();

将十进制数作为 JSON 数字传递会更正确,但很明显这不适合您.

It would be more correct to pass the decimal as a JSON number, but it's clear that's not going to be an option for you.