【问题标题】:Passing JSON to Web.API works with Fiddler but not in code将 JSON 传递给 Web.API 适用于 Fiddler,但不适用于代码
【发布时间】: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


【解决方案1】:

您尚未发布您的接收方法代码,但根据提供的数据,它应该是一个带有一个参数的方法,该参数是一个代表您的 JSON 的对象。在这种情况下,您根本不需要使用FromBody 属性。

如果您查看此article,您可以在那里找到:

默认情况下,Web API 使用以下规则绑定参数:

  • 如果参数是“简单”类型,Web API 会尝试从 URI 中获取值。简单类型包括 .NET 原始类型(int,
    bool、double 等),以及 TimeSpan、DateTime、Guid、decimal、
    和字符串,加上任何带有类型转换器的类型,可以从 一个字符串。
  • 对于复杂类型,Web API 尝试使用媒体类型格式化程序从消息正文中读取值。

我根据你的 JSON 创建了一个模型:

public class Address
{
    public int Id { get; set; }
    public string State { get; set; }
    public string City { get; set; }
}

public class RootObject
{
    public int Id { get; set; }
    public int Count { get; set; }
    public string StartDate { get; set; } // Keeped as string for simplicity
    public Address Address { get; set; }
}

然后是可以接收这种JSON的非常简单的方法:

public RootObject Post(RootObject req)
{
    return req;
}

然后我用 Fiddler 测试了它:

Method: 
  POST
Headers:
  Content-Type: application/json
Request Body:
  {"Id":0,"Count":0,"StartDate":"\\/Date(-62135596800000)\\/","Address":{"Id":0,"State":"test","City":"test"}} 

和C#代码:

var data = "{\"Id\":0,\"Count\":0,\"StartDate\":\"\\/Date(-62135596800000)\\/\",\"Address\":{\"Id\":0,\"State\":\"test\",\"City\":\"test\"}}";

using (var client = new HttpClient())
{
    HttpContent content = new StringContent(data);
    content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

    HttpResponseMessage response =
        client.PostAsync("http://my.url", content).Result;
    var result = response.Content.ReadAsStringAsync().Result;
}

在这两种情况下,我都能将对象送回。

一些提示:

  • 当您使用 Fiddler 发送 JSON 时,您应该使用任何转义 请求正文。只需输入有效的 JSON 即可。
  • 在 C# 代码中,如果您需要使用 JSON 声明字符串变量
    将需要使用转义。例如var json = "\"a\":\"b\"";var json = @"""a"":""b""";。如果您从某个地方收到 JSON 否则,您无需执行任何操作。
  • 切勿使用 ' 字符来封装 JSON。

【讨论】:

  • 感谢人,这是问题所在。当我将我的 web api post 方法更改为: public string Post([FromBody]MyObjectModel value) 时,它工作正常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-20
  • 1970-01-01
  • 2021-10-27
  • 2020-08-03
相关资源
最近更新 更多