.Net Core 3.1 为何数据FromBody 接收实体参数为NULL的惨案

众所周知在.net core 里面这样使用很普遍 

但是为何在3.1当中是NULL呢?

public async Task<IActionResult> 方法名称([FromBody]CreateMapDto dto)
{ }

是这样微软的“天才”程序员在.net core 3.1 中Newtonsoft.Json 替换为了System.Text.Json 

在.NET Core 3之前,ASP.NET Core在内部使用了Newtonsoft.Json,现在它使用的是System.Text.Json。

 一个例子是一个数字字段,例如MyClass.Value1。如果从Javascript中传递“10”或10,Newtonsoft.Json将处理这个问题并将两者识别为10。默认情况下,对于System.Text.Json,该字段不能有引号,如果在Json中发布了引号,则会得到一个空的[FromBody]值。

解决方案 注入 AddNewtonsoftJson 引用Newtonsoft.Json

public void ConfigureServices(IServiceCollection services)
{
    services.AddRazorPages().AddNewtonsoftJson();

    //All your other code:
    //...
}

 

posted @ 2020-05-11 15:31  渐变大理石  阅读(2147)  评论(4编辑  收藏  举报