WebAPI Selfhost:无法将多个参数绑定到请求的内容 [英] WebAPI Selfhost: Can't bind multiple parameters to the request's content

查看:41
本文介绍了WebAPI Selfhost:无法将多个参数绑定到请求的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码被简化以显示必要性.我可以知道怎么了吗?我似乎无法使用[FromBody]属性检索两个参数(在这种情况下为A和B).

The below code are simplified to show the necessity. May I know what is wrong? I can't seems to retrieve two Parameters (A and B in this case) using the [FromBody] attribute.

错误消息是无法将多个参数('A'和'B')绑定到请求的内容"

The error message is "Can't bind multiple parameters ('A' and 'B') to the request's content"

如果我只有A或B,那就太好了.

It is perfectly fine if I have either A or B only.

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;

推荐答案

我认为Web Api不支持多个[FromBody]参数.但是您可以使用Api模型将更多参数传递给api操作.

Web Api doesn't support multiple [FromBody] params I think. But you can use Api model, to passing more parameters to your api action.:

public class YourApiModel
{
    public int A{ get; set; }

    public int B { get; set; }

    //...other properties    
}

之后,您可以在API控制器测试中简单地使用它:

After that, you can simply use this in your API controller Test:

    // POST: api/test
    public IHttpActionResult Post([FromBody] YourApiModel model)
    {
        //do something
    }

希望有帮助.

这篇关于WebAPI Selfhost:无法将多个参数绑定到请求的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆