I have added two GET methods into webapi as follow:
public IList<ProductDTO> GetAllProducts()
{
ProductManager pm = new ProductManager();
return pm.RetrieveAllProducts();
//return pm.RetrieveAllProducts();
}
public ProductDTO GetProduct(int i)
{
ProductManager pm = new ProductManager();
return pm.RetrieveAllProducts().Where(c => c.Id == i).FirstOrDefault();
//return pm.RetrieveAllProducts();
}
Problem, when i only kept one get method GetAllProducts()
then it works fine. but when i added GetProduct(int i)
then giving me error as Not found 404
error.
Please guide me how i could keep both the mthod and allow to access method having argument.
calling as follow:
$.ajax({
type: 'GET',
url: 'api/values/GetProduct/1', //giving error as NOT FOUND
contentType: 'json',
dataType: 'json',
success: function (data) {
$.each(data, function (key, value) {
//stringify
alert(key);
alert(value);
var jsonData = JSON.stringify(value);
//Parse JSON
var objData = $.parseJSON(jsonData);
var id = objData.Id;
var Cost = objData.Cost;
var ProductName = objData.ProductName;
var Description = objData.Description;
$('<tr><td>' + id + '</td><td>' + ProductName +
'</td><td>' + Cost + '</td></tr>').appendTo('#ProductDivWebApi');
});
},
error: function (xhr) {
alert(xhr.responseText);
}
});
i have added this WEBAPI into MVC 4 project.
its route shows as below:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "ProductManagement", action = "Index", id = UrlParameter.Optional }
);
}
Please guide