2014-07-05 43 views
0

這是我的API控制器張貼至網絡API的對象始終是空

[HttpPost] 
[CheckModelForNull] 
[ValidateModelState] 
public HttpResponseMessage Post([FromBody] DepModel depModel) 
{ 
    try 
    { 

     var entity = TheDepModelFactory.Parse(depModel); 

     if (entity == null) Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not read object from body"); 

     TheDepRepository.Post(entity); 

      return Request.CreateResponse(HttpStatusCode.Created, TheDepModelFactory.Create(entity)); 

    } 
    catch (Exception ex) 
    { 

     return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex); 
    } 
} 

POST方法這是我的模型

public class DepModel 
{ 

     public int dep_Id { get; set; } 
     public string dep_name { get; set; } 

} 

當我發送使用招POST請求將其添加到數據庫,但是當我使用我的應用程序時,它不起作用。

我的教程 http://www.asp.net/web-api/overview/creating-web-apis/using-web-api-with-entity-framework/part-9 下載應用程序,當我跑我給了我同樣的問題,該應用程序可以從小提琴手添加到數據庫中而不是從應用程序視圖?!

我認爲這是在配置不是在代碼中的東西,但我無法弄清楚。

回答

0

設置正確的「的Content-Type」的頭部

+0

此應用程序在另一臺計算機的問題是在我的電腦不在應用程序工作正常。我使用Windows 8.1 64位,Visual Studio 2012更新1.我禁用了防火牆,以防止它阻止請求,但沒有解決問題。 – user1275681