Web Api FromBody is null from web client

妖精的绣舞 提交于 2019-12-05 02:20:30

I think it has more to do with the fact that you technically aren't passing in a string. You are passing in a JSON serialized string representation of an anonymous type, so the deserialization process in the Web Api is working against you. By the time your request gets to the controller and that method, it isn't a string anymore. Try changing your type on the SavImage method to be dynamic. Like this:

public string SavImage([FromBody]dynamic data)
{
      JObject jObject = JObject.Parse(data);
      JObject version = (JObject)jObject["version"];

      return "-OK-" + version;
}

Unfortunately at that point you won't be able to use intellisense to get your properties out. You will have to get the data out of the dynamic type via a dictionary.

Dictionary<string, object> obj = JsonConvert.DeserializeObject<Dictionary<string, object>>(Convert.ToString(data));

Of course your other option would be to use an actual type that is shared between the client and the server. That would make this a bit easier.

The string value passed in the body probably needs to be prefixed with the = sign.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!