Boas, eu tenho aqui um problema com a minha API e gostaria de pedir a vossa ajuda. Tenho aqui um formulário em Knockout JS, faço serialize de um constructor que por sua vez me devolve uma querystring assim
API?Page=1&MinPrice=185&MaxPrice=694
O meu problema é que a API não entende o que é isto e devolve me isto
{"message":"An error has occurred.","exceptionMessage":"Multiple actions were found that match the request: \r\nGet on type SiteMvc.Controllers.ProductAPIController\r\nGet on type SiteMvc.Controllers.ProductAPIController","exceptionType":"System.InvalidOperationException","stackTrace":" em System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext)\r\n em System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)\r\n em System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"}
ListVM
public class ListVM
{
public decimal? MinPrice { get; set; }
public decimal? MaxPrice { get; set; }
public int? CategoryID { get; set; }
public int ProductType { get; set; }
}
API Controller
public HttpResponseMessage Get(int page, [FromUri]ListVM vm)
{
CompanyManager m = new CompanyManager();
HttpResponseMessage r = CheckEntryJson(m);
if (r != null)
return r;
ProductsBL bl = new ProductsBL(m.CompanyID);
var query = bl.GetProducts();
if (vm.MinPrice.HasValue)
query = query.Where(x => x.ProductStore.Price >= vm.MinPrice);
if (vm.MaxPrice.HasValue)
query = query.Where(x => x.ProductStore.Price <= vm.MaxPrice);
if (vm.CategoryID.HasValue)
query = query.Where(x => x.CategoryID == vm.CategoryID);
Func<ProductType, string> getProductTypeName = x => x.ToName();
var result = query.Select(x => new ProductAPIModel
{
Name = x.Name,
TotalPrice = x.ProductStore == null ? 0 : x.ProductStore.TotalPrice,
ID = x.ProductID,
ProductType = getProductTypeName(x.ProductType),
Reference = x.Reference,
Description = x.Description
});
if (result == null) return Request.CreateResponse(HttpStatusCode.NotFound);
var json = new
{
Items = result.Skip(pageSize * (page - 1)).Take(pageSize).ToList(),
TotalItems = result.Count()
};
return Request.CreateResponse(HttpStatusCode.OK, json);
}
}
O meu objectivo é fazer com que a API perceba que isto são campos do model sem ter que recorrer a pôr os campos todos à mão como parâmetros e/ou mexer nas routes
Obrigado desde já !