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

查看:42
本文介绍了如何在 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"
  }
}

使用带有默认路由的 API/Entity/someid?culture=en&layout=1 等 URL 将数据发布到 ASP.NET MVC4 Web API.

Data is posted to ASP.NET MVC4 Web API using URL like API/Entity/someid?culture=en&layout=1 with default routing.

headerDatarowData 值属性在运行时定义并且可以变化.

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.

culturelayout 查询字符串参数是可选的.

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?

推荐答案

如果JSON的结构没有变化

有了它,您就可以像在 url 中提供的一样发送正文API/Entity/someid?culture=en&layout=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 总是像你提到的那个,那么应该自动反序列化这样的东西:

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 访问 JSON 元素的帖子.

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 ...;
}

您还可以阅读this,了解发布原始数据的其他替代方案数据到 webapi 控制器.

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

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

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