AngularJs $资源没有调用自定义' GET'

时间:2017-11-27 12:14:41

标签: angularjs asp.net-web-api2 asp.net-web-api-routing angular-resource

我在.Net中创建了一个简单的RESTful服务,托管here。 我试图从我的angularjs代码中调用getByName动作。但是,angularjs代码正在调用默认的' get'而不是' getByName'

AngularJs代码:

app.factory('UserResourceSvc',function($resource){
    var baseApiUrl = "http://publicapitest.azurewebsites.net/";
    //var baseApiUrl = "http://localhost:92/"
    return $resource( baseApiUrl + 'api/Employee/:id',{id: "@id"},
        { 
            getByName : {method: 'GET', params: {} , isArray: false} 
        }
    );    
});

我在按钮单击下调用下面的函数来触发API调用。

 $scope.getByName = function(){
   UserResourceSvc.getByName(function(data){
      debugger;
   })
 }

我收到以下错误消息:

angular.js:14794 Error: [$resource:badcfg] Error in resource configuration for action `getByName`. Expected response to contain an object but got an array (Request: GET http://publicapitest.azurewebsites.net/api/Employee)
http://errors.angularjs.org/1.6.7/$resource/badcfg?p0=getByName&p1=object&p2=array&p3=GET&p4=http%3A%2F%2Fpublicapitest.azurewebsites.net%2Fapi%2FEmployee
    at angular.js:116
    at $http.then.response.resource (angular-resource.js:757)
    at processQueue (angular.js:17145)
    at angular.js:17193
    at Scope.$digest (angular.js:18331)
    at Scope.$apply (angular.js:18628)
    at done (angular.js:12619)
    at completeRequest (angular.js:12863)
    at XMLHttpRequest.requestLoaded (angular.js:12780) "Possibly unhandled rejection: {}"

当我查询提琴手时,电话会转到'http://publicapitest.azurewebsites.net/api/Employee'而不是'http://publicapitest.azurewebsites.net/api/Employee/getByName'

我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

您应该按如下方式指定方法网址:

getByName : {method: 'GET', url: baseApiUrl + 'api/Employee/getByName', params: {} , isArray: false}