I have two or more get methods in web api, (as per below give code GetProducts and GetProductsNew are get methods). As i am using the ng-resource of angular js, whenever i try to call get function, it is giving an ambiguous error. Suppose i want to call GetProductsNew method then how can i call? Can anyone help please, thanks in advance :)
// 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" }
}
);
});
By 'ambiguous error' I'm guessing that you are getting :"Multiple actions were found that match the request...."? This is because you can not have multiple Get
methods with the same signature in the same controller.
Try adding these attributes to the methods in your webapi
public class ProductController : ApiController
{
[HttpGet]
[Route(Name = "GetProducts")]
public HttpResponseMessage GetProducts()
{
}
[HttpGet]
[Route(Name = "GetProductsNew")]
public HttpResponseMessage GetProductsNew()
{
}
}
You need to install AspNet.WebApi.WebHost if you have not already done so:
Install-Package Microsoft.AspNet.WebApi.WebHost
This article has a nice explanation to attribute routing