Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在控制器上找不到与请求AngularJS POST错误匹配的操作_Angularjs_Http Post_Asp.net Web Api - Fatal编程技术网

在控制器上找不到与请求AngularJS POST错误匹配的操作

在控制器上找不到与请求AngularJS POST错误匹配的操作,angularjs,http-post,asp.net-web-api,Angularjs,Http Post,Asp.net Web Api,我对AngularJS有个误会。另外,我对后端的WebApi不是很熟悉,但我尝试过。 我有一个模式表单,点击按钮(提交)即可显示。我希望在单击“提交”时更新数据库中的某些内容。所以我想到了一个帖子 AppModule.factory('editResult', function($http) { return { postResult: function(id, res) { return $http.post('/api/MatchesAdmin/'+id, res);

我对AngularJS有个误会。另外,我对后端的WebApi不是很熟悉,但我尝试过。 我有一个模式表单,点击按钮(提交)即可显示。我希望在单击“提交”时更新数据库中的某些内容。所以我想到了一个帖子

AppModule.factory('editResult', function($http)
{
return {
    postResult: function(id, res) {
        return $http.post('/api/MatchesAdmin/'+id, res);
    }
};
});
此服务应该执行实际的发布(我在控制器的Submit函数中调用postResult)。 我没有在AppModule.config中设置任何内容,因为我认为没有必要设置它。。。 WebApi控制器(MatchesAdminController)操作如下所示:

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

        return Request.CreateResponse(HttpStatusCode.OK);
    }
AppModule.factory('editResult', function($http)
{
return {
    putResult: function(id, res) {
        return $http.put('/api/MatchesAdmin/PutMatch/' + id+'/'+res);
    }
  };
 });
然而,我在其他情况下也像这样玩,这很有效。但现在,可能是因为模态形式,而不是舒尔,它说:

未找到与请求URI匹配的HTTP资源…./api/MatchesAdmin/1'

在与请求匹配的控制器“MatchesAdmin”上未找到任何操作(但存在该操作)


为什么呢?我还检查了WebApi.config,看起来还不错……

嗯,我终于得到了答案。我其实不知道该找什么。问题是我没有正确配置WebApi.config。我看了更多关于路由的内容,在stackoverflow上偶然发现了以下答案:

[1]

对于多个参数,我不知道这与webApi路由有关。 因为这是一个更新,所以我将帖子更改为PUT。工厂修改如下:

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

        return Request.CreateResponse(HttpStatusCode.OK);
    }
AppModule.factory('editResult', function($http)
{
return {
    putResult: function(id, res) {
        return $http.put('/api/MatchesAdmin/PutMatch/' + id+'/'+res);
    }
  };
 });
另外,在WebApi.config中,我现在有:

 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 }
          );
好吧,我不是舒尔这完全正确,但它现在起作用了