Reputation: 51
Something weird is happening .... and I don't really understand why.... This post works only if change the id parameter type from int to object.
I know the best way is to set the value in the url... however I tried to avoid this thing... my preference is to post values as json
public IHttpActionResult Gettest(int id) = don't work !
{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:23052/api/testAPI/Gettest'.","MessageDetail":"No action was found on the controller 'testAPI' that matches the request."}
public IHttpActionResult Gettest(object id) = it works
this runs nice
What am I doing wrong ?.... I tried to change the parameter name from "id" to "x" on server/client sides and I got the same result.
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);
});
angular post
Upvotes: 4
Views: 75
Reputation: 1326
Change your $http request to below. No need to write action method name, it will get called by controller name only. Web api works based on HTTP VERBS so just replace your request with below code:
$http.post("/api/testAPI/"+ id)
.success(function (result) {
$scope.test = result;
}).error(function (result) {
console.log(result);
});
Upvotes: 0