在c# mvc中接收RESTClient POST JSON-Body并将其转换为可用的数据

本文关键字:转换 数据 JSON-Body mvc POST RESTClient | 更新日期: 2023-09-27 18:10:18

我有一个c#网站和Web API组合和一个移动Android应用程序。在移动Android应用程序中,我想发送一个带有List<Integer>, List<Double>long的HttpPost到c# Web API,在c# Web API中,我想将它们转换回List<int>, List<decimal>long

Android应用程序的发送部分完成得相当快,因为我以前做过这个。但是在接收c# Web API端,我遇到了(很多)麻烦。

在其中一个Web API控制器中,我添加了以下方法:
[HttpPost]
[AllowAnonymous]
[ActionName("save")]
public bool SaveOrders([FromBody] STILL_UNDETERMINED parameter)
{
    // TODO: Convert given parameter to usable List<int>, List<decimal> and long
}

现在我正在使用FireFox的RESTClient插件来发送这些HttpPOSTs:

Method:   POST   http://localhost:54408/api/orders/save
Header:   Content-Type   application/x-www-form-urlencoded
Body:     {"newPrices":[0.4,7.4],"productIds":[20,25],"dateInTicks":1402459200000}

我确实在上面的方法中结束并可以调试它,但现在我需要将其转换为List, List和long。

所以,我的问题是:

我应该为这个方法使用什么参数?我试过了:

  • string(总是null);
  • JObject(由于某种未知的原因,:" {"取代了所有[,导致:{ "{'"newPrices'":": { "0.4,7.4],'"productIds'":": { "20,25],'"dateInTicks'":1402459200000}": "" } } };
  • dynamic(我不能做任何事情,因为它的转换到一个看似空的object)

如何将此参数转换为List<int>, List<decimal>long ?

  • 我试过创建一个自定义对象(见下文),并使用各种不同的方法来填充它们,许多人在这里列出所有,所有没有结果(主要是因为参数已经错了):

自定义对象:

Data
{
       public List<int> productIds { get; set; }
       public List<decimal> newPrices { get; set; }
       public long dateInTicks { get; set; }
}

理论上看起来很简单:

  • 从FireFox的RESTClient插件发送HttpPOST
  • 在API控制器中有一个接收它的方法(这已经花费了相当多的时间,并且必须通过使用actions对MapHttpRoutes进行一些更改)。
  • 将您从Body接收到的参数转换为可用数据(我现在卡住的部分…)

它看起来很简单,但我不能让它在c#中工作。在Java中,我会在不到30分钟的时间里做到这一点,我可以通过谷歌搜索找到很多有用的答案,但是当我谷歌c# HttpPOSTs时,我只得到结果,要么从c#发送HttpPOSTs,要么从发送HttpPOST后的流中读取响应。在c# mvc中,没有任何关于从[HttpPost]方法接收HttpPOST的JSON-body .

PS:我在我的配置文件中有以下内容,只接收JSON格式的响应,也许这是错误的。

// Only allow JSON response format
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);

在c# mvc中接收RESTClient POST JSON-Body并将其转换为可用的数据

当将JSON数据发送到MVC Action时,您需要指定Content-Type头为application/json。如果你不这样做,模型将永远是null,如果你传递任何东西,而不是一个标准的表单集合。

[HttpPost]
[AllowAnonymous]
[ActionName("save")]
public bool SaveOrders(Data model)
{
   //model is populated, and you have access to ModelState
}
public class Data
{
    // the JSON to Model mapper match is case-insensitive
    public List<int> ProductIds { get; set; }
    public List<decimal> NewPrices { get; set; }
    public long DateInTicks { get; set; }
}

Post数据:

{"newPrices":[0.4,7.4],"productIds":[20,25],"dateInTicks":1402459200000}

所以您应该能够将其作为模型对象传入。首先设置你的模型类:

public class Order
{
    public List<int> productIds { get; set; }
    public List<decimal> newPrices { get; set; }
    public long dateInTicks { get; set; }
}

然后设置你的动作使用模型绑定到:

[HttpPost]
public ActionResult SaveOrders(Order order)
{
    //TODO: Save...
}

然后您所要做的就是确保将其作为与您的Order模型匹配的JSON对象发送进来。因此,post值将是["productIds"], ["newPrices"]等等,它将自动填充SaveOrders上的order参数。这就很好地说明了这些东西是如何绑定的:

http://weblogs.asp.net/nmarun/asp-net-mvc-2-model-binding-for-a-collection