2015-02-24 73 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}