眾所周知在.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: //... }