Unable to select object by ID in .net Odata implementation

// GET api/Product/5 public Product GET([FromODataUri]int id) { Product product = db.Products.Find(id); if (product == null) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound)); } return product; } 

this is above to get the object with id is never called with my url:

 http://localhost:53208/odata/Product(1) 

even with the default settings in the odata route, this is not called.

At first I tried with odata route settings:

  config.Routes.MapODataRoute("ODataRoute", "odata", GetEdmModel()); 

Remember that my simple β€œGet with requests” query works fine. but this is the only thing that works, and the PUT method works. others all do not work. This is a kind of controller. I tried for about a day .. please help.

 public class ProductController : ODataController { private OfferAssistantDbContext db = new OfferAssistantDbContext(); // GET api/Product public IQueryable<Product> GET() { return db.Products.AsQueryable<Product>(); } // GET api/Product/5 public Product GET([FromODataUri]int id) { Product product = db.Products.Find(id); if (product == null) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound)); } return product; } 
+7
rest api odata asp.net-mvc-4
source share
1 answer

OData Web APIs are distinguished by parameter names. The parameter name must be key instead of id ie

 public Product GET([FromODataUri]int key) { } 
+8
source share

All Articles