【问题标题】:How to receive dynamic data in WebAPI controller如何在 WebAPI 控制器中接收动态数据
【发布时间】:2016-02-29 20:13:08
【问题描述】:

ASP.NET MVC4 WebAPI Post 控制器在 POST 缓冲区中接收 json 数据。 Object 包含两个固定名称属性,headerData 和 rowData。 它们具有可变数量的属性,例如

{"headerData": {
    "Tasudok": "134",
    "Kuupaev": "2015-11-23",
    "Dokumnr": "135319"
   },


"rowData": {
  "Toode":"",
  "Kogus":"0.0000",
  "Nimetus":"öäölä<a",
  "_rowsum":"0.00",
  "Id":"1639",
  "Dokumnr":"135319",
  "_oper":"edit",
  "_rowid":"1639"
  }
}

例如,在某些调用中,rowData 可能包含其他属性,而某些 rowData 属性可能会丢失。 使用带有默认路由的 API/Entity/someid?culture=en&layout=1 等 URL 将数据发布到 ASP.NET MVC4 Web API。

如何在 WebAPI 控制器中接收参数?

我按照How to receive dynamic data in Web API controller Post method试了一下

public class EntityController : APIController
{

    public class Body
    {   Dictionary<string, string> headerData { get; set; }
        Dictionary<string, string> rowData { get; set; }
    }

    public HttpResponseMessage Post(string id, Body body,
        [FromUri]string culture = null,
        [FromUri]uint layout = 0
        )
    { ... }
}

但是 body.headerData 和 body.rowData 有是空的。 如何将值放入其中?

【问题讨论】:

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


    【解决方案1】:

    代替

    public class Body
    {   
        Dictionary<string, string> headerData { get; set; }
        Dictionary<string, string> rowData { get; set; }
    }
    

    明确地将您的类成员标记为公共,以便反序列化程序可以完成它的工作!

    以下应该有效:

    public class Body
    {   
        public Dictionary<string, string> headerData { get; set; }
        public Dictionary<string, string> rowData { get; set; }
    }
    

    另外,指定 id 将来自 url:

    public HttpResponseMessage Post(Body body,
        [FromUri]string id, 
        [FromUri]string culture = null,
        [FromUri]uint layout = 0
    )
    

    【讨论】:

      猜你喜欢
      • 2016-02-28
      • 1970-01-01
      • 2017-01-20
      • 1970-01-01
      • 1970-01-01
      • 2016-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多