使用angularjs $ resource

时间:2015-08-28 18:00:14

标签: javascript c# .net angularjs ngresource

这是我的Web Api行动:

[RoutePrefix("api/CustomTemplate")]
public class CustomTemplateController : ApiController
{
    [HttpGet]
    [Route("GetCustomTemplate")]
    public IHttpActionResult GetCustomTemplate()
    {
         //Code 
    }
}

这是我的angularjs文件:

服务文件:

'use strict';
app.factory('customService', ['$resource', 'ngAuthSettings', function ($resource, ngAuthSettings) {

var serviceBase = ngAuthSettings.apiServiceBaseUri;

return $resource(serviceBase + 'api/CustomTemplate/', {}, {
    query: { method: 'GET', isArray: true },
    getCustomTemplate: {
        url: 'GetCustomTemplate',
        method: 'GET',
        isArray: false
    }
});
}]);

我的angularjs控制器:

'use strict';
app.controller('customController', [
    '$scope', 'customService', function ($scope, customService) 
    {
        customService.getCustomTemplate({},function (customTemplate) 
        {
            $scope.customTemplate = customTemplate;
        });        
    }
]);

我的问题是,我无法从angularjs调用GetCustomTemplate。请有人能告诉我我做错了什么吗?

1 个答案:

答案 0 :(得分:3)

您必须调用完整网址

'use strict';
app.factory('customService', ['$resource', 'ngAuthSettings', function ($resource, ngAuthSettings) {

var serviceBase = ngAuthSettings.apiServiceBaseUri;

return $resource(serviceBase + 'api/CustomTemplate/', {}, {
    query: { method: 'GET', isArray: true },
    getCustomTemplate: {
        url: serviceBase +'api/CustomTemplate/GetCustomTemplate', //full URL + custom action
        method: 'GET',
        isArray: false
    }
});
}]);