Asp.net-Web-Api

WebApi - 傳遞一組值

  • November 10, 2017

我需要使用 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?

簡短的回答

要發送小數數組,WebApi 需要像這樣的 url 簽名:GET http://localhost:50668/api/test/calculate?Operand1=1.0&Operand1=2.0&Operand2=3.0&Operand2=4.0

該網址將發送

$$ 1.0,2.0 $$作為 Operand1 和$$ 3.0,4.0 $$作為操作數 2。 長答案

通過使用 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 客戶端,它應該如下所示: 使用 GET 方法查詢

使用 GET,數據通過 URL 發送

請注意,如果您使用 GET 方法,伺服器將期望從 URL 接收輸入。因此,您應該發送如下查詢:GET 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 客戶端,它應該如下所示: 使用 POST 方法查詢

邊注

用於生成 SwaggerUI(列印螢幕)的 nuget 包可以在此處找到。在 WebApis 上執行臨時測試非常有用。

引用自:https://stackoverflow.com/questions/34314306