1

I have an AngularJS misunderstanding. Also, I am not really familiar with WebApi for backend, but I try. I have a modal form wich displays at a click of a button(Submit). I want that when I click 'Submit', to update something in the database. So I thaught of a POST.

AppModule.factory('editResult', function($http)
{
return {
    postResult: function(id, res) {
        return $http.post('/api/MatchesAdmin/'+id, res);
    }
};
});

This service should do the actual posting(I call postResult in Submit function in the controller). I did not set anything in the AppModule.config as I thaught there is no need for it... The WebApi controller(MatchesAdminController) action looks like this:

   [HttpPost]
    public HttpResponseMessage PostMatch(int id,string result)
    {
        MatchDTO match= _matchService.GetById(id);
        match.Result = result;
        _matchService.Update(match);

        return Request.CreateResponse(HttpStatusCode.OK);
    }

However I played like this in other contexts and it worked. But now, probably because of the modal form, not shure, it says that:

No HTTP resource was found that matches the request URI ...../api/MatchesAdmin/1'

No action was found on the controller 'MatchesAdmin' that matches the request(but there is the action)

Why is that? I also checked WebApi.config and it seemed fine...

anne
  • 43
  • 2
  • 8

1 Answers1

1

Well, I finally got the answer. I actually did not know what to look for. The problem was that I wasn't configuring WebApi.config right. I looked more about routing, and I stumbled upon this answer on stackoverflow, here:

[1] Web API routing with multiple parameters

I did not know that it had to do with webApi routing, for multiple parameters. Because it was an update, I changed the POST into a PUT.The factory is modified like this:

AppModule.factory('editResult', function($http)
{
return {
    putResult: function(id, res) {
        return $http.put('/api/MatchesAdmin/PutMatch/' + id+'/'+res);
    }
  };
 });

Also, in WebApi.config I now have this:

 config.Routes.MapHttpRoute("UpdateMatchResult", "api/{controller}/{action}/{id}/{result}",
            new
            {
                id=UrlParameter.Optional,
                result=UrlParameter.Optional
            });

            config.Routes.MapHttpRoute(
              name: "DefaultApi",
              routeTemplate: "api/{controller}/{id}",
              defaults: new { id = RouteParameter.Optional }
          );

Well, I am not shure this is entirely correct, but it works for now....

Community
  • 1
  • 1
anne
  • 43
  • 2
  • 8