[ASP.NET] WebAPI Post Mapping 複雜型別

  • 5198
  • 0

[ASP.NET] WebAPI Post Mapping 複雜型別

在前一篇中提到在GET方式中使用複雜型別,然而比起GET ,在實務上在POST的使用更會需要用到複雜型別,例如透過POST寫入一筆員工資料到資料庫中,而員工資料所包含的欄位資料當然不會只有一個姓名,所以在WebAPI Post 我們可能會這麼設計,在POST參數裡使用一個複雜型別

   1: [HttpPost]
   2: public String EmpInert(EmpInfo empinfo)
   3: {
   4:     //do insrt db 
   5:  
   6:     //為了demo用,直接return value
   7:     return empinfo.EmpNo;
   8: }

 

接著在Client Device的部份,我們可以利用JSON Formatter來Mapping

   1: //use JSON Formatter
   2:  
   3: string data = "{\"EmpNo\":\"0001\",\"Name\":\"Ian\",\"Tel\":\"0800123456\"}";
   4:  
   5: System.Uri uri = new Uri("http://192.168.1.17:20139/webapi/Emp/EmpInert");
   6: HttpWebRequest apireq = (HttpWebRequest)HttpWebRequest.Create(uri);
   7: apireq.Method = "POST";
   8: apireq.ContentType = "application/json";
   9: byte[] byteArray = Encoding.UTF8.GetBytes(data);
  10: apireq.ContentLength = byteArray.Length;
  11:  
  12: using (Stream dataStream = apireq.GetRequestStream())
  13: {
  14:     dataStream.Write(byteArray, 0, byteArray.Length);
  15: }
  16:  
  17: using (WebResponse response = apireq.GetResponse())
  18: using (Stream dataStream = response.GetResponseStream())
  19: using (StreamReader reader = new StreamReader(dataStream))
  20: {
  21:     string responseFromServer = reader.ReadToEnd();
  22:     Response.Write(responseFromServer);
  23: }

 

測試結果正確Mapping EmpInfo.EmpNo

image

 

除此之外,我們也可以利用[FromUri]的方式,藉由參數傳遞的方式,來做型別的Mapping,首先把POST的設計改成如下,注意在參數的部份加上了[FromUri]關鍵字(在前一篇文章中筆者有提及到WebAPI預設針對複雜型別是採用[FromBody])

   1: [HttpPost]
   2: public String EmpInert([FromUri]EmpInfo empinfo)
   3: {
   4:    //do insrt db 
   5:  
   6:    //為了demo用,直接return value
   7:    return empinfo.EmpNo;
   8: }

 

接著在Client Device的部份,我們可以利用uri夾帶參數的方式,進行型別的Mapping

   1: //use url parameter
   2:  
   3: StringBuilder uristr = new StringBuilder();
   4: uristr.Append("?EmpNO=0001");
   5: uristr.Append("&Name=Ian");
   6: uristr.Append("&Tel=0800123456");
   7:  
   8:  
   9: System.Uri uri = new Uri("http://192.168.1.17:20139/webapi/Emp/EmpInert" + uristr);
  10: HttpWebRequest apireq = (HttpWebRequest)HttpWebRequest.Create(uri);
  11: apireq.Method = "POST";
  12: apireq.ContentType = "application/x-www-form-urlencoded";
  13: byte[] byteArray = Encoding.UTF8.GetBytes(uristr.ToString());
  14: apireq.ContentLength = byteArray.Length;
  15:  
  16: using (Stream dataStream = apireq.GetRequestStream())
  17: {
  18:     dataStream.Write(byteArray, 0, byteArray.Length);
  19: }
  20:  
  21: using (WebResponse response = apireq.GetResponse())
  22: using (Stream dataStream = response.GetResponseStream())
  23: using (StreamReader reader = new StreamReader(dataStream))
  24: {
  25:     string responseFromServer = reader.ReadToEnd();
  26:     Response.Write(responseFromServer);
  27: }

 

測試結果正確Mapping EmpInfo.EmpNo

image

 

上述二種方式都可以讓我們進行Mapping,筆者仍建議採用第一種方式以JSON Formatter來進行處理,原因很簡單,這種方式並不需要在網址參數夾帶過多的資訊,此外JSON Formatter已是大家所熟知的通用格式,當然如果您的呼叫請求由網頁來進行的話,那麼您大可以採用jQuery方式直接套用JSON就可以輕鬆搞定,但Client Device若是Mobile Native App的話,則可以使用上述例子以HttpWebRequest配合JSON Formatter來進行處理,也是相當容易的喔。

 

若本文對您有所幫助,歡迎轉貼,但請在加註【轉貼】及來源出處,並在附上本篇的超連結,感恩您的配合囉。

By No.18