使用angularjs $resource从web api调用自定义方法

本文关键字:api 调用 自定义方法 web angularjs resource 使用 | 更新日期: 2023-09-27 18:10:12

这是我的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
    }
});
}]);

my angularjs controller:

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

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

使用angularjs $resource从web api调用自定义方法

必须调用完整URL

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