What is the problem here? webapi + angular + message

Something strange is happening ... and I really don’t understand why ... This message only works if I change the type of the id parameter 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 the values ​​as json

public IHttpActionResult Gettest (int id) = not working!

{"Message": "No HTTP resource was found that matches the request URI" http: // localhost: 23052 / api / testAPI / Gettest '. "," MessageDetail ":" No actions were found on the controller "testAPI" that match the request. "}

public IHttpActionResult Gettest (object identifier) ​​= it works

it works well

What am I doing wrong? .... I tried changing the parameter name from "id" to "x" on the server / client side, 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

+4
source share
1 answer

Modify your $ http request below. No need to write the name of the action method, it will be called only by the name of the controller. The web api is based on HTTP VERBS, so just replace your request with the code below:

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

Source: https://habr.com/ru/post/1665572/


All Articles