asp.net - .NET Core Web API : multiple [FromBody]?

标签 asp.net asp.net-mvc asp.net-core .net-core asp.net-core-mvc

我正在将用 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/

相关文章:

asp.net - 如何在 ASPDotNetStoreFront 中为产品添加子产品?

ASP.NET:是否有一种简单的方法可以在多个母版页中包含一个 div 实例?

asp.net - 如何在gridview中将列设置为只读

json - 使用 System.Text.Json 高效替换大型 JSON 的属性

asp.net-core - UrlRewrite 中间件和客户端应用程序

c# - 访问模板化用户控件 ASP.NET 中的子控件

.net - 如何使用户从唯一的一台机器(通过获取 CPU 串行)登录到 ASP.NET-MVC Web 应用程序

jquery - 向每个 jqGrid ajax 调用添加参数

c# - 错误说 system.void 不能从 c# 使用

c# - 如何在 ASP.NET Core MVC 中启用跨源请求 (CORS)