我在web api中有两个或更多get方法(如下所示,代码GetProducts和GetProductsNew是get方法)。因为我正在使用角度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" }
}
);
});
答案 0 :(得分:0)
出现模糊错误'我猜你得到了:"找到了与请求匹配的多个操作...." ?这是因为在同一个控制器中不能有多个具有相同签名的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属性路由