我正在将用 ASP.NET MVC 编写的代码转换为 ASP.NET Core MVC。在转换代码时,我遇到了一个问题。我们使用了一个具有多个参数的方法,如下所示:
[HttpPost]
public class Search(List<int> ids, SearchEntity searchEntity)
{
//ASP.NET MVC
}
但是在 .NET Core 中编码时,
ids
参数为 null
.[HttpPost]
public class Search([FromBody]List<int> ids,[FromBody]SearchEntity searchEntity)
{
//ASP.NET Core MVC
}
当我放置
ids
SearchEntity
中的参数上课,没有问题。但是我有很多这样写的方法。我能做些什么来解决这个问题?
最佳答案
只能有一个FromBody
因为正文只能读取一次
引用 Model Binding in ASP.NET Core
There can be at most one parameter per action decorated with
[FromBody]
. The ASP.NET Core MVC run-time delegates the responsibility of reading the request stream to the formatter. Once the request stream is read for a parameter, it's generally not possible to read the request stream again for binding other[FromBody]
parameters.
MVC Core 对如何将模型绑定(bind)到 Action 更加严格。当您想要自定义绑定(bind)行为时,您还必须明确指出要绑定(bind)数据的位置。
关于asp.net - .NET Core Web API : multiple [FromBody]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50552702/