2017-01-03 90 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); 
     });