如何使用 agluar ng-resource 在 web api 中调用特定的 get 方法?
How to call specific get method in web api using agluar ng-resource?
我在网络中有两个或多个获取方法 api,(按照下面给出的代码,GetProducts 和 GetProductsNew 是获取方法)。当我使用 angular js 的 ng-resource 时,每当我尝试调用 get 函数时,它都会给出一个不明确的错误。假设我想调用 GetProductsNew 方法,我该如何调用?任何人都可以帮忙吗,提前致谢:)
// This is my web api code
public class ProductController : ApiController
{
...
public HttpResponseMessage GetProducts()
{
}
public HttpResponseMessage GetProductsNew()
{
}
}
// **This is my angular**
// This is the code in controller.js, using which i am calling the get method from above code, but here the problem is, since the above web api is having the two gt method, i am getting the "Multiple actions were found that match the request"
productcatControllers.controller('ProductListCtrl', ['$scope', '$routeParams',
'$location', '$route', 'productService',
function ($scope, $routeParams, $location, $route, productService)
{
productService.query(function (data) {
$scope.products = data;
});
}]);
// This is the service of angular js
var phonecatServices = angular.module('productcatServices', ['ngResource']);
phonecatServices.factory("productService", function ($resource) {
return $resource(
"/api/Product/:id",
{ id: "@id" },
{
"update": { method: "PUT" }
}
);
});
来自 'ambiguous error' 我猜你得到的是:"Multiple actions were found that match the request...."?这是因为在同一控制器中不能有多个具有相同签名的 Get
方法。
尝试将这些属性添加到您的 webapi 中的方法
public class ProductController : ApiController
{
[HttpGet]
[Route(Name = "GetProducts")]
public HttpResponseMessage GetProducts()
{
}
[HttpGet]
[Route(Name = "GetProductsNew")]
public HttpResponseMessage GetProductsNew()
{
}
}
您需要安装 AspNet.WebApi.WebHost 如果您还没有安装:
Install-Package Microsoft.AspNet.WebApi.WebHost
这篇文章有个不错的explanation属性routing
我在网络中有两个或多个获取方法 api,(按照下面给出的代码,GetProducts 和 GetProductsNew 是获取方法)。当我使用 angular js 的 ng-resource 时,每当我尝试调用 get 函数时,它都会给出一个不明确的错误。假设我想调用 GetProductsNew 方法,我该如何调用?任何人都可以帮忙吗,提前致谢:)
// This is my web api code
public class ProductController : ApiController
{
...
public HttpResponseMessage GetProducts()
{
}
public HttpResponseMessage GetProductsNew()
{
}
}
// **This is my angular**
// This is the code in controller.js, using which i am calling the get method from above code, but here the problem is, since the above web api is having the two gt method, i am getting the "Multiple actions were found that match the request"
productcatControllers.controller('ProductListCtrl', ['$scope', '$routeParams',
'$location', '$route', 'productService',
function ($scope, $routeParams, $location, $route, productService)
{
productService.query(function (data) {
$scope.products = data;
});
}]);
// This is the service of angular js
var phonecatServices = angular.module('productcatServices', ['ngResource']);
phonecatServices.factory("productService", function ($resource) {
return $resource(
"/api/Product/:id",
{ id: "@id" },
{
"update": { method: "PUT" }
}
);
});
来自 'ambiguous error' 我猜你得到的是:"Multiple actions were found that match the request...."?这是因为在同一控制器中不能有多个具有相同签名的 Get
方法。
尝试将这些属性添加到您的 webapi 中的方法
public class ProductController : ApiController
{
[HttpGet]
[Route(Name = "GetProducts")]
public HttpResponseMessage GetProducts()
{
}
[HttpGet]
[Route(Name = "GetProductsNew")]
public HttpResponseMessage GetProductsNew()
{
}
}
您需要安装 AspNet.WebApi.WebHost 如果您还没有安装:
Install-Package Microsoft.AspNet.WebApi.WebHost
这篇文章有个不错的explanation属性routing