【问题标题】:Call custom method from web api using angularjs $resource使用 angularjs $resource 从 web api 调用自定义方法
【发布时间】:2015-08-28 18:00:14
【问题描述】:

这是我的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。请问有人能告诉我我做错了什么吗?

【问题讨论】:

    标签: javascript c# .net angularjs ngresource


    【解决方案1】:

    您必须调用完整的 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
        }
    });
    }]);
    

    【讨论】:

    • 声明使用 serviceBase + 'api/CustomTemplate/'。谢谢:)
    猜你喜欢
    • 2016-05-19
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    • 1970-01-01
    • 1970-01-01
    • 2016-06-14
    • 2015-05-18
    • 1970-01-01
    相关资源
    最近更新 更多