Search code examples
angularjsasp.net-web-api2asp.net-web-api-routingangular-resource

AngularJs $resource not invoking custom 'GET'


I have created a simple RESTful service in .Net which is hosted here. I am trying to invoke getByName action from my angularjs code. But, angularjs code is invoking default 'get' instead of 'getByName'

AngularJs code:

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

I am invoking below function on button click to trigger API call.

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

I am getting below error message :

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: {}"

When I checked in fiddler call is going to 'http://publicapitest.azurewebsites.net/api/Employee' instead of 'http://publicapitest.azurewebsites.net/api/Employee/getByName'

Am I missing anything?


Solution

  • You should specify method url as follows:

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