使用 angularjs $resource 从 Web api 调用自定义方法
Call custom method from web api using angularjs $resource
这是我的 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
。有人可以告诉我我做错了什么吗?
你必须打满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
}
});
}]);
这是我的 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
。有人可以告诉我我做错了什么吗?
你必须打满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
}
});
}]);