我将AngularJS与MVC4一起使用。当我点击data-ng-grid中的一行时,我希望它从我的Web API调用GET方法。但是......我想让它调用GET BY ID方法。由于某种原因,它总是调用GET方法来检索所有电影。如何调用使用param id的GET方法?不确定我做错了什么。
var movieResource = $resource('/api/movie', {}, { update: { method: 'PUT' }, getById: { method: 'GET', params: 'id' } });
$scope.$watchCollection('selectedMovies', function () {
//$scope.selectedMovie = angular.copy($scope.selectedMovies[0]);
movieResource.getById(5);
});
public MovieData Get(int id)
{
var movie = movieRepository.GetDataById(id);
if (movie == null)
{
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
}
return movie;
}
MovieController
// GET api/movie
public IEnumerable<MovieData> Get()
{
return movieRepository.GetAllData().AsEnumerable();
}
// GET api/movie/5
public MovieData Get(int id)
{
var movie = movieRepository.GetDataById(id);
if (movie == null)
{
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
}
return movie;
}
答案 0 :(得分:1)
根据$resource documentation,你必须提供这样的参数:movieResource.get({id: 5})
您也可以使用$ http服务$http.get('/api/movie/'+id)