简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM