2014-02-24 26 views
2

我有一個非常簡單的WebAPI控制器(所有系統默認值),當我對它發佈信息時,事實上它具有Content-Type:application/json (從小提琴手)掛起(不返回)。當調用WebAPI發佈應用程序/ JSON標頭時沒有反應

我的頭如下:

Content-Length: 2 
Content-Type: application/json 

和後體就是

[] 

我的WebAPI控制器看起來就像這樣:

namespace WebAPI.rest 
{ 
public class SendGridController : ApiController 
{ 

    public HttpResponseMessage Post() 
    { 
     try 
     { 
      HttpContent requestContent = Request.Content; 
      string json = requestContent.ReadAsStringAsync().Result.Trim(); 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 

     return new HttpResponseMessage(HttpStatusCode.OK); 

    } 

當我做出同樣的帖子(與提琴手)到​​它沒有問題返回

回答

1

原來,這個問題是一個老版本的WebAPI的。一旦我更新到webapi2,問題就消失了。

1

如果您在ASP.NET下運行,.Result可能不是一個明智的想法。我在自主主機下運行你的代碼,它工作正常。

試試這個,

public class SendGridController : ApiController 
{ 

public async Task<HttpResponseMessage> Post() 
{ 
    try 
    { 
     HttpContent requestContent = Request.Content; 
     string json = await requestContent.ReadAsStringAsync(); 
    } 
    catch (Exception ex) 
    { 
     throw ex; 
    } 

    return new HttpResponseMessage(HttpStatusCode.OK); 

} 

}

相關問題