Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cant select an entity 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 above of getting entity with ID is never called with my url :

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

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

first i was trying with this odata route settings:

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

remember my simple Get with queries is working fine. but this is the only thing which is working, and PUT method is working. other all are not working. this is view of controller. I have 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;
    }
like image 958
ahmadalibaloch Avatar asked Dec 03 '13 09:12

ahmadalibaloch


1 Answers

Web API OData is particular with parameter names. The parameter name should be key instead of id i.e.

public Product GET([FromODataUri]int key)
{
}
like image 185
RaghuRam Nadiminti Avatar answered Oct 21 '22 23:10

RaghuRam Nadiminti