what's the issue here?? webapi + angular + post

Question

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


Show source
| c#   | angularjs   | asp.net-mvc   | asp.net-web-api   2017-01-03 03:01 1 Answers

Answers to what's the issue here?? webapi + angular + post ( 1 )

  1. 2017-01-03 05:01

    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);
            });
    

Leave a reply to - what's the issue here?? webapi + angular + post

◀ Go back