在ASP.NET Core 3.1中,[FromBody]特性的行为发生了更改,可能会导致一些问题。在2.2版本中,[FromBody]特性默认会读取请求正文中的数据并进行模型绑定,但在3.1版本中,默认情况下,[FromBody]特性不会读取表单数据。
要解决这个问题,可以使用以下两种方法之一:
[ApiController]
public class MyController : ControllerBase
{
[HttpPost]
public IActionResult MyAction([FromBody] MyModel model)
{
// 处理模型数据
return Ok();
}
}
public class MyModel
{
public string Property1 { get; set; }
public string Property2 { get; set; }
}
public class MyController : ControllerBase
{
[HttpPost]
public IActionResult MyAction([FromForm] MyModel model)
{
// 处理模型数据
return Ok();
}
}
使用上述方法之一,就能在ASP.NET Core 3.1中解决[FromBody]特性的问题。