WebApi - 传递值数组

时间:2015-12-16 14:15:45

标签: asp.net-web-api

我需要使用ASP.NET Web API(版本4.5.2)构建API。首先,我只想创建一个添加一些数字的基本端点。为了做到这一点,我创建了:

[RoutePrefix("api/test")]
public class MyController : ApiController
{
  [HttpGet]
  public IEnumerable<int> Calulate(decimal[] op1, decimal[] op2)
  {
    var results = new List<Calculation>();
    for (var i=0; i<op1.Length; i++) 
    {
      var calculation = new Calculation();
      calculation.Operand1 = op1[i];
      calculation.Operand2 = op2[i];
      calculation.Calculate();

      results.Add(calculation);
    }
    return results;
  }

  public class Calculation
  {
    public int Operand1 { get; set; }
    public int Operand2 { get; set; }
    public int Result { get; set; }

    public void Calculate()
    {
      this.Result = this.Operand1 + this.Operand2;
    }
  }
}

我现在正试图通过Postman Chrome应用程序点击此终结点。当我通过Postman运行它时,我收到了一个错误。这就是我正在做的事情:

在Postman中,我将“http://localhost:50668/api/test/calculate”放在“GET”下拉列表旁边的URL字段中。然后我点击“发送”。我收到以下错误:

{
  "Message": "An error has occurred.",
  "ExceptionMessage": "Can't bind multiple parameters ('op1' and 'op2') to the request's content.",
  "ExceptionType": "System.InvalidOperationException",
  "StackTrace": "..."
} 

我认为(我不知道)原因是因为我没有正确地将值传递给Postman的API。但是,我不知道该怎么做。如何将值数组传递给API?

3 个答案:

答案 0 :(得分:10)

简短回答

要发送小数组数组,WebApi需要url签名,如: 获取http://localhost:50668/api/test/calculate?Operand1=1.0&Operand1=2.0&Operand2=3.0&Operand2=4.0

该url将发送[1.0,2.0]作为Operand1和[3.0,4.0]作为Operand2。

长答案

通过使用GET http://localhost:50668/api/test/calculate调用您的api,您实际上不会向您的服务器发送任何内容。 (除标题内容外)

如果要将数据发送到服务器,则至少有2个选项:

选项2:如果操作是幂等的,则使用GET方法 就像William Xifaras已经指出的那样,指定您的输入将来自URL,以便WebApi正确解释。为此,请使用[FromUri]。

    [HttpGet]
    [Route("calculate")]
    public List<Calculation> CalculateWithGet([FromUri]decimal[] Operand1, [FromUri]decimal[] Operand2)
    {
        var results = new List<Calculation>();
        for (var i = 0; i < Operand1.Length; i++)
        {
            var calculation = new Calculation();
            calculation.Operand1 = Operand1[i];
            calculation.Operand2 = Operand2[i];
            calculation.Calculate();

            results.Add(calculation);
        }
        return results;
    }

    public class Calculation
    {
        public decimal Operand1 { get; set; }
        public decimal Operand2 { get; set; }
        public decimal Result { get; set; }

        public void Calculate()
        {
            Result = this.Operand1 + this.Operand2;
        }
    }

使用REST客户端,它应该如下所示: Query using GET method

使用GET,数据通过URL

发送

请注意,如果您使用GET方法,服务器将接收来自URL的输入。因此,您应该发送以下查询: 获取http://localhost:50668/api/test/calculate?op1=1.0&op1=2.0&op2=3.0&op2=4.0

如果操作不是幂等的,则使用POST方法

由于操作进行了一些服务器端计算,我假装它可能并不总是幂等的。如果是这种情况,POST可能更合适。

    [HttpPost]
    [Route("calculate")]
    public List<Calculation> CalculateWithPost(CalculationInputs inputs)
    {
        var results = new List<Calculation>();
        for (var i = 0; i < inputs.Operand2.Length; i++)
        {
            var calculation = new Calculation();
            calculation.Operand1 = inputs.Operand1[i];
            calculation.Operand2 = inputs.Operand2[i];
            calculation.Calculate();

            results.Add(calculation);
        }
        return results;
    }

    public class CalculationInputs
    {
        public decimal[] Operand1 { get; set; }
        public decimal[] Operand2 { get; set; }
    }

    public class Calculation
    {
        public decimal Operand1 { get; set; }
        public decimal Operand2 { get; set; }
        public decimal Result { get; set; }

        public void Calculate()
        {
            Result = this.Operand1 + this.Operand2;
        }
    }

使用POST,数据通过正文

发送

使用该结构,服务器希望从请求正文接收输入。如果正文与函数的签名匹配,WebApi将反序列化正文。

使用REST客户端,它应该如下所示: Query using POST method

旁注

用于获取生成的SwaggerUI(printscreens)的nuget包可以找到here。在WebApis上运行adhoc测试非常有用。

答案 1 :(得分:4)

在参数之前从[FromUri]添加。

public IEnumerable<int> Calulate([FromUri] decimal[] op1, [FromUri] decimal[] op2)
  

要强制Web API从URI中读取复杂类型,请添加   [FromUri]属性为参数

http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

答案 2 :(得分:0)

我认为你可以作为JSON数组传递

http://localhost:50668/api/test/calculate?op1=[1,2,3]&op2=[4,5,6]

希望这有帮助