Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何为POST方法设置webapi控制器_C#_Asp.net_Asp.net Web Api - Fatal编程技术网

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

C# 如何为POST方法设置webapi控制器,c#,asp.net,asp.net-web-api,C#,Asp.net,Asp.net Web Api,我正在尝试创建一个POST服务,该服务返回Tienda列表。我的代码如下所示: [HttpPost] [ResponseType(typeof(List<TiendaWrapper>))] public IHttpActionResult GetTiendasPost([FromBody]Tienda t) { List<Tienda> ListaTiendas = db.Tiendas.ToList(); List<TiendaWrapper>

我正在尝试创建一个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);
}
任何帮助都会很好,提前谢谢你

编辑:

这是我在Postman中调用方法的方式:
我将Body中的值添加为表单数据。

在HTTP请求中,您需要将内容类型设置为:
内容类型:application/json
如果您将Body作为表单数据传递,则应使用
application/x-www-form-urlencoded
作为媒体类型。当然,除非你真的在发送多部分数据。但是,我不确定默认情况下WebAPI是否设置为处理多部分表单。

您可以分享一下如何使用postman进行发布吗?您在postman中的内容类型是什么?如何调用此方法。显示用户界面/小提琴手/邮递员code@RenatoEspinozaCarranza. 您需要为内容类型添加标题。例如,如果您的内容类型是JSON,那么在postman的headers选项卡中,您应该添加-content-type:application/jsonRead more here@Taleeb我这样做了,但是参数“t”抛出了一个空指针异常。我不知道如何将值发送给它。
"$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",