簡體   English   中英

如何為POST方法設置WebAPI控制器

[英]How to setup a webapi controller for POST method

我正在嘗試使POST服務返回Tienda列表。 我的代碼如下所示:

[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作為主體並找到具有這些Id的Tiendas

"$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",

任何幫助都會很棒,在此先感謝您。

編輯:

這就是我在郵遞員中調用該方法的方式: http:// localhost:1918 / api / tiendas / GetTiendasPost然后我將Body中的值添加為表單數據。

在HTTP請求中,您需要將Content-Type設置為: Content-Type: application/json

如果將正文作為表單數據傳遞,則應使用application/x-www-form-urlencoded作為媒體類型。 當然,除非您確實要發送多部分數據。 但是,我不確定默認情況下WebAPI是否設置為處理多部分表單。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM