为什么复杂的字符串(字符串化的json数据)不被接受为Web API方法的POST参数,而是简单的字符串? [英] Why is a complex string (stringified json data) not accepted as a POST parameter to a Web API method, but a simple string is?
问题描述
我想将一堆数据(作为字符串转换为json)发送到Web API POST方法.我可以发送一个简单的字符串就好了,但是当我尝试发送字符串化的json数据时,甚至没有达到该方法-显然,该复杂字符串没有被视为有效的字符串值或类似的东西.
I want to send a bunch of data, converted to json, as a string, to a Web API POST method. I can send a simple string just fine, but when I try to send stringified json data, the method is not even reached - apparently the complex string is not viewed as a valid string value or something.
当从客户端传递"randomString"时,此方法有效:
This works, when passing "randomString" from the client:
[Route("{unit}/{begindate}/{enddate}/{stringifiedjsondata}")]
[HttpPost]
public void Post(string unit, string begindate, string enddate, string stringifiedjsondata)
{
// test
string jsonizedData = stringifiedjsondata;
WinForms
string dataAsJson = "randomString";
String uriToCall = String.Format("/api/produceusage/{0}/{1}/{2}/{3}", _unit, beginRange, endRange, dataAsJson);
HttpResponseMessage response = await client.PostAsync(uriToCall, null);
当字符串是json数据时,例如:
When the string is json data, such as this:
[
{
"ItemDescription": "DUCKBILLS, GRAMPS-EIER 70CT 42#",
"PackagesMonth1": 1467, . . . }]
...它不起作用.我通过使用JSON.NET将通用列表转换为json来创建此字符串,如下所示:
...it does not work. I create this string by converting a generic list to json using JSON.NET like so:
string dataAsJson = JsonConvert.SerializeObject(_rawAndCalcdDataAmalgamatedList, Formatting.Indented);
String uriToCall = String.Format("/api/produceusage/{0}/{1}/{2}/{3}", _unit, beginRange, endRange, dataAsJson);
HttpResponseMessage response = await client.PostAsync(uriToCall, null);
所以唯一的区别是字符串;当它像"randomString"这样简单时,我在Web API POST方法中到达以下行:
So the only difference is in the string; when it's something simple like "randomString" I reach this line in the Web API POST method:
string jsonizedData = stringifiedjsondata;
...但是当它是复杂的字符串(例如,字符串化的json数据)时,则不会到达该行.
...but when it's a complex string, such as stringified json data, that line is not reached.
为什么?我该如何修复字符串化的json数据,以便接收并识别它?
Why? And how can I fix the stringified json data so that it is received and recognized?
推荐答案
您遇到了麻烦,因为您正在通过URL将JSON发送到您的API.我建议您在请求的正文中发送它.为此,请像这样更改您的Web Api方法:
You are having troubles because you are sending the JSON to your API through the URL. I suggest you to send it in the body content of your request. In order to do it, change your Web Api method like this:
[Route("{unit}/{begindate}/{enddate}")]
[HttpPost]
public void Post(string unit, string begindate, string enddate, [FromBody] string stringifiedjsondata)
{
// test
string jsonizedData = stringifiedjsondata;
}
还要更改您的Winforms客户端代码:
Also change your Winforms client code:
string dataAsJson = "[{\"ItemDescription\": \"DUCKBILLS, GRAMPS-EIER 70CT 42#\",\"PackagesMonth1\": 1467}]";
String uriToCall = String.Format("/api/produceusage/{0}/{1}/{2}", _unit, beginRange, endRange);
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("", dataAsJson)
});
HttpResponseMessage response = await client.PostAsync(uriToCall, content);
现在它应该可以按预期工作.检查此链接关于HTTP客户端库的信息,以查看有关在需要时向API发送数据的更多示例.
Now it should work as you expected. Check this link about the HTTP Client library in order to see more examples about sending data to an API if you need it.
这篇关于为什么复杂的字符串(字符串化的json数据)不被接受为Web API方法的POST参数,而是简单的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!