如何为POST方法设置webapi控制器

时间:2015-09-27 00:42:03

标签: c# asp.net asp.net-web-api

我正在尝试创建一个返回Tienda列表的POST服务。我的代码如下所示:

[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);
}

但是当我使用Postman调用服务时,我得到了这个例外。该函数应该接收两个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",

任何帮助都会很棒,谢谢你。

编辑:

这就是我在Postman中调用方法的方法:http://localhost:1918/api/tiendas/GetTiendasPost 我将Body中的值添加为form-data。

2 个答案:

答案 0 :(得分:1)

在HTTP请求中,您需要将Content-Type设置为:Content-Type: application/json

答案 1 :(得分:1)

如果您将正文作为表单数据传递,则应使用application/x-www-form-urlencoded作为媒体类型。除非你真的发送多部分数据。但是,我不确定WebAPI是否设置为默认处理多部分表单。