以下代码经过简化以显示必要性。我可以知道出了什么问题吗?我似乎无法使用[FromBody]属性检索两个参数(在本例中为A和B)。
错误消息是&#34;无法将多个参数(&#39; A&#39;&#39; B&#39;)绑定到请求的内容&#34; < / p>
如果我只有A或B,那就完全没问题了。
Web API:
[Route("API/Test"), HttpPost]
public IHttpActionResult Test([FromBody] int A, [FromBody] int B)
客户端:
HttpClient client = new HttpClient();
var content = new FormUrlEncodedContent(
new Dictionary<string, string> {
{ "A", "123" },
{ "B", "456" }
});
client.PostAsync("http://localhost/API/Test", content).Result;
答案 0 :(得分:41)
我认为Web Api并不支持多个[FromBody]参数。但是你可以使用Api模型,将更多参数传递给你的api动作。:
public class YourApiModel
{
public int A{ get; set; }
public int B { get; set; }
//...other properties
}
之后,您可以在API控制器测试中使用它:
// POST: api/test
public IHttpActionResult Post([FromBody] YourApiModel model)
{
//do something
}
希望有所帮助。
答案 1 :(得分:6)
尝试使用Web API代码:
[DataContract]
public class Model
{
[DataMember]
public int A { get; set; }
[DataMember]
public int B { get; set; }
}
[Route("API/Test"), HttpPost]
public IHttpActionResult Test([FromUri] Model model)