2015-02-24 79 views
0

我试图绑定POST数据到如下对象:阅读JSON对象中的ASP.NET Web API

[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()); 

我使用招测试:

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。任何想法?

回答

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}