2017-01-03 70 views
4

發生了一些不可思議的事情......並且我不太明白爲什麼...... 只有將id參數類型從int更改爲object,此帖才能工作這裏有什麼問題? webapi + angular + post

我所知道的最好的方法是在URL中設置的值...但是,我試圖避免這件事情......我的偏好是張貼值JSON

公共IHttpActionResult Gettest(INT ID) =不工作!

{「消息」:「沒有HTTP資源發現匹配的請求URI‘http://localhost:23052/api/testAPI/Gettest’。」,「MessageDetail」:「無動作被在控制器‘testAPI’與請求匹配找到」}

公共IHttpActionResult Gettest(對象ID)=它的工作原理

這個運行不錯

我是什麼樂隊g錯了?....我嘗試在服務器/客戶端將參數名稱從「id」更改爲「x」,並得到了相同的結果。

config.Routes.MapHttpRoute("DefaultActionApi", "api/{controller}/{action}/{id}", new { id = RouteParameter.Optional }); 

WebApiConfig.cs

 [HttpPost] 
    public IHttpActionResult Gettest(int id) 
    { 
     test test = db.tests.Find(id); 
     if (test == null) 
     { 
      return NotFound(); 
     } 

     return Ok(test); 
    } 

testAPIController.cs

 $http.post("/api/testAPI/Gettest", id) 
    .success(function (result) { 


     $scope.test = result; 


    }).error(function (result) { 
     console.log(result); 
    }); 

角度支柱

+1

如果您嘗試使用「int?'而不是? – juharr

+0

它沒有工作...:S ...我剛剛試過 –

+0

更改爲公共IHttpActionResult Gettest([FromUri] int id) –

回答

0

更改您的$ HTTP請求之下。無需編寫操作方法名稱,它將僅通過控制器名稱進行調用。 Web api基於HTTP VERBS工作,因此只需用下面的代碼替換您的請求:

$http.post("/api/testAPI/"+ id) 
     .success(function (result) { 
      $scope.test = result; 
     }).error(function (result) { 
      console.log(result); 
     });