【问题标题】:How to call GET WEB API method from AngularJS如何从 AngularJS 调用 GET WEB API 方法
【发布时间】:2014-04-08 18:55:15
【问题描述】:

我将 AngularJS 与 MVC4 一起使用。当我单击 data-ng-grid 中的一行时,我希望它从我的 Web API 调用 GET 方法。但是...我希望它调用 GET BY ID 方法。出于某种原因,它总是调用检索所有电影的 GET 方法。如何调用使用参数 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;
    }

电影控制器

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

【问题讨论】:

    标签: angularjs asp.net-mvc-4 asp.net-web-api


    【解决方案1】:

    根据$resource documentation,您必须提供如下参数:movieResource.get({id: 5})

    你也可以使用 $http 服务$http.get('/api/movie/'+id)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-14
      • 1970-01-01
      • 2020-01-24
      • 2015-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多