如何接收的Web API控制器Post方法的动态数据 [英] How to receive dynamic data in Web API controller Post method

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

问题描述

jqGrid的帖子在POST请求缓存JSON数据作为

jqgrid posts json data in POST request buffer as

{"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"
  }
}

数据使用URL喜欢张贴到ASP.NET MVC4的Web API API /实体/ someid文化= EN&放大器;?布局= 1 使用默认路由

headerData rowData value属性在运行时定义,可能会有所不同。

headerData and rowData value properties are defined in runtime and can vary.

例如在某些通话rowData可能含有额外的属性和一些rowData性能可能会丢失。

For example in some call rowData may contain additional properties and some rowData properties may be missing.

文化布局查询字符串PARAMATERS都是可选的。

culture and layout query string paramaters are optional.

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

How to receive parameters in WebAPI controller ?

我试过

public class EntityController : APIController
{

public class PostParams {
    public string culture { get; set; }
    public int? layout { get; set; }
    }

    public HttpResponseMessage Post(string id, 
      [FromUri]PostParams optionalParams,
      [FromBody]IList<NameValue> headerData,
      [FromBody]IList<NameValue> rowData )
    { ... }


public class NameValue
{
    public string name, value;
}
}

但headerData和rowData是空的。如何让所有的参数?

But headerData and rowData are empty. How to get all parameters?

推荐答案

有了这将允许你发送的身体像你一个网址提供诸如一
?API /实体/ someid文化= EN&放大器;布局= 1

If the structure of the JSON does not change

Having this will permit you to send the body like the one you provided at a url like API/Entity/someid?culture=en&layout=1.

要在您的控制器路径指定可选查询参数,让他们像一个默认值

public class EntityController : APIController
{
    public HttpResponseMessage Post([FromUri]string culture="EN", [FromUri]int layout=1, YourBody body )
    { ... }
}

如果YourBody总是喜欢你mentionned的人,这样的事情应该是自动反序列化:

If YourBody is always like the one you mentionned, something like this should be deserialized automatically:

public class YourBody
{
    public Dictionary<string, string> HeaderData {get; set;}
    public Dictionary<string, string> RowData{get; set;}
}

和会给你完全进入身体的任何部分。

and would give you full access to any element of the body.

像这样将允许接受任何形式的JSON的:

Something like this would permit the receive any kind of json:

public HttpResponseMessage  Post([FromBody]JToken body)
{
    // Process the body
    return ...
}

您将需要一些额外的验证,因为没有反序列化对象将作出。唯一想你会知道的是,你的身体是一个JSON。

You will need some extra validation since no object deserialization will be made. The only think you'll know is that your body is a JSON.

您可能因此要分析它,看看它看起来像你期待什么。
请参见那个帖子有关如何使用JToken

You therefore may want to parse it to see if it looks like what you are expecting. See that post about how to access element of a JSON with JToken.

例如,你可以不喜欢下面来处理不断变化的主体内容,仍然处理您的路线可选的查询参数:

For instance, you could do something like the following to handle a changing body content and still handle optional query parameters of your route :

public HttpResponseMessagePost([FromBody]JToken body, [FromUri]string culture="EN", [FromUri]int layout=1)
{
    JObject headerData= body["headerData"].Value<JObject>();
    JObject headerData= body["rowData"].Value<JObject>();
    return ...;
}

您还可以读取其他办法原始数据发布到的WebAPI控制器。

You may also read this about other alternatives for posting raw data to a webapi controller.

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

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