2014-09-22 55 views
1

我有一个AngularJS误解。另外,我对后端WebApi并不熟悉,但我尝试了一下。 我有一个模式的形式,点击一个按钮(提交)显示。我希望当我点击'提交'时,更新数据库中的内容。所以我想要一个POST。在控制器上找不到与请求相匹配的操作AngularJS POST错误

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

该服务应该做实际的发布(我在控制器的提交功能中调用postResult)。 我没有在AppModule.config设置什么,因为我thaught没有必要为它... 的的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); 
    } 

但是我在其他情况下打出这样的它的工作。但现在,模态形式,可能是因为,没有舒尔,它说:

没有HTTP资源发现,请求匹配URI ...../API/MatchesAdmin/1'

行动未在匹配请求的控制器'MatchesAdmin'上找到(但有动作)

这是为什么?我也检查了WebApi.config,它似乎很好...

回答

1

那么,我终于得到了答案。我其实不知道该找什么。问题是我没有正确配置WebApi.config。我看了更多的路由,我偶然发现了这个答案的计算器,在这里:

[1] Web API routing with multiple parameters

我不知道,它必须做的WebAPI路由,多参数。 因为这是一个更新,我改变了POST到PUT.The工厂被修改如下:

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

嗯,我我不舒服这是完全正确的,但它现在的作品....

相关问题