在ASP.NET Web API中从POST读取JSON对象

时间:2015-02-24 07:41:29

标签: asp.net .net asp.net-web-api

我试图将POST数据绑定到object,如下所示:

[Route("ta_iba/test")]
[HttpPost]
public string test(Photo p)
{
     ............
}

public class Photo
{
    public string url { get; set; }
    public string caption { get; set; }
    public int width { get; set; }
    public int height { get; set; }
}
WebApiConfig:     
        config.Formatters.Add(new JsonMediaTypeFormatter());

我正在使用fiddler进行测试:

Header:
User-Agent: Fiddler
ContentType: application/json
Host: localhost:50653
Content-Length: 50
Body:
{"url":"xxxx","caption":"yyy","width":50,"height":50}

但我的错误为HTTP/1.1 415 Unsupported Media Type。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您需要将请求参数发送为Content-Type,而不是ContentType

Header:
User-Agent: Fiddler
Content-Type: application/json
Host: localhost:50653
Content-Length: 50
Body:
{"url":"xxxx","caption":"yyy","width":50,"height":50}