2015-09-27 53 views
0

我試圖做一個POST服務,返回一個列表Tienda。我的代碼看起來像這樣:如何設置POST方法的webapi控制器

[HttpPost] 
[ResponseType(typeof(List<TiendaWrapper>))] 
public IHttpActionResult GetTiendasPost([FromBody]Tienda t) 
{ 
    List<Tienda> ListaTiendas = db.Tiendas.ToList(); 
    List<TiendaWrapper> lstTiendas = new List<TiendaWrapper>(); 

    foreach(Tienda T in ListaTiendas) 
    { 
     if (T.CodDpto == t.CodDpto && T.CodRetail == t.CodRetail) 
     { 
      TiendaWrapper tiend = new TiendaWrapper(T); 
      lstTiendas.Add(tiend); 
     } 
    } 

    return Ok(lstTiendas); 
} 

但是當我使用郵遞員調用服務時,我得到這個異常。該功能應該接收兩個標識的是身體,發現有這些標識的

"$id": "1", 
    "Message": "The request entity's media type 'multipart/form-data' is not supported for this resource.", 
    "ExceptionMessage": "No MediaTypeFormatter is available to read an object of type 'Tienda' from content with media type 'multipart/form-data'.", 
    "ExceptionType": "System.Net.Http.UnsupportedMediaTypeException", 

任何幫助將是巨大的Tiendas,預先感謝您。

編輯:

這是我如何調用郵差方法:http://localhost:1918/api/tiendas/GetTiendasPost 我在體爲形式的數據添加值。

+1

你可以請分享你如何使用郵遞員?郵遞員的內容是什麼? – dotnetstep

+0

你是怎麼稱呼這種方法的。顯示UI/Fiddler /郵遞員代碼 – Taleeb

+2

@RenatoEspinozaCarranza。您需要爲內容類型添加標題。例如,如果您的內容類型是JSON,那麼在郵遞員的標題選項卡中,您應該添加 - Content-Type:application/json – Taleeb

回答

1

在您需要的內容類型設置爲HTTP請求:Content-Type: application/json

1

如果您正在過身體表單數據,那麼你應該使用application/x-www-form-urlencoded作爲介質類型。除非你真的在發送多部分數據。但是,我不確定WebAPI默認設置爲處理多部分表單。

相關問題