如何接收的WebAPI控制器的动态数据 [英] How to receive dynamic data in WebAPI controller

查看:162
本文介绍了如何接收的WebAPI控制器的动态数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET MVC4的WebAPI控制器后在POST缓冲区接收JSON数据。
对象包含两个固定名称的属性,headerData和rowData。
他们有不同数量像

ASP.NET MVC4 WebAPI Post controller receives json data in POST buffer. Object contains two fixed name properties, headerData and rowData. They have have variable number of properties like

{"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 /实体/ someid文化= EN&放网址张贴到ASP.NET MVC4的Web API;布局= 1 使用默认路由

For example in some call rowData may contain additional properties and some rowData properties may be missing. Data is posted to ASP.NET MVC4 Web API using URL like API/Entity/someid?culture=en&layout=1 with default routing.

如何的WebAPI控制器接收参数?

How to receive parameters in WebAPI controller ?

我根据试过<一个href=\"http://stackoverflow.com/questions/33924458/how-to-receive-dynamic-data-in-web-api-controller-post-method\">How以接收网络API控制器Post方法动态数据

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有是空的。
如何获取到它们的值?

But body.headerData and body.rowData have are empty. How to get values into them?

推荐答案

而不是

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

明确你的类成员标记为公开,以便解串器可以到它的工作!
使用此,它应该工作:

explicitly mark your class members as public so the deserializer can to its job ! Use this and it should work:

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

和也,指定ID将来自网址:

And also, specify that id will come from the url:

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

这篇关于如何接收的WebAPI控制器的动态数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆