无法将JSON对象从Rest Client传递到MVC4控制器C#

时间:2016-03-22 14:44:57

标签: c# json asp.net-mvc-4

我从Postman RestClient调用Controller操作并传递对象,但我收到的值为0.

请求网址:http://localhost:27266/ImageProcessing/CreateThumbnail

POST数据:

{
"data":{
    "Width":140
    "Height":140
    }
}   

[HttpPost]
[ValidateInput(false)]
public JsonResult CreateThumbnail(InputDataModel data )
{
  return Json(new { Height = "0"});
}

其中InputDataModel是

public class InputDataModel 
{
    public int Width { get; set; }

    public int Height { get; set; }
}

我收到的宽度和高度为0.

我错过了什么。?

1 个答案:

答案 0 :(得分:0)

我认为映射器不会将整个对象树拖网映射 - 需要告诉它在哪里看,例如:

public class YourModel
{
    public InputDataModel data { get; set; }
}

public class InputDataModel 
{
    public int Width { get; set; }

    public int Height { get; set; }
}

或者您可以调整JSON以符合InputDataModel,因此它应该有两个名为WidthHeight的成员...而不是嵌套在data属性中