【发布时间】:2015-04-20 20:27:12
【问题描述】:
我正在尝试将我的 JSON 传递给 Web.API 服务。当我设置为 POST 并且我在 [FromBody ] 参数中获得值时,发送与 Fiddler 配合得很好:
Http/1.1
User-Agent: Fiddler
content-type: application/json; charset=utf-8
Host: http://localhost:27701/api/myList
Content-Length: 883
但是当我使用这个 C# 代码发布 JSON 时,[FromBody ] 参数为空:
HttpContent content = new StringContent(data);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:27701/api/");
HttpResponseMessage response = client.PostAsync("myList", content).Result;
if (response.IsSuccessStatusCode)
{
var result = response.Content.ReadAsAsync<string>().Result;
s = result;
}
数据部分是 Fiddler 和我的代码中的确切 JSON,以及两个调用中调用的控制器。
这是我的 JSON:
{
"Id":0,
"Count":0,
"StartDate":"\\/Date(-62135596800000)\\/",
"Address":{
"Id":0,
"State":"test",
"City":"test"
}
}
一件事是,如果我不在提琴手内的字符串两侧放置'(单引号),[FromBody] 参数为空,但如果我将它们放在 C# 示例中,则响应为 500 服务器错误。
【问题讨论】:
-
可以添加接收Web.API方法代码吗?
标签: c# json asp.net-web-api