Dot-Net
無法在 .net Odata 實施中按 id 選擇實體
// 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; }使用我的 url 永遠不會呼叫上述獲取具有 ID 的實體的方法:
http://localhost:53208/odata/Product(1)即使在 odata 路由中使用預設設置,也不會呼叫它。
首先我嘗試使用這個 odata 路由設置:
config.Routes.MapODataRoute("ODataRoute", "odata", GetEdmModel());請記住我的簡單 Get 查詢工作正常。但這是唯一有效的方法,並且 PUT 方法有效。其他都不起作用。這是控制器的視圖。我已經嘗試了大約一天..請幫忙。
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; }
Web API OData 特別適用於參數名稱。參數名稱應該
key代替idiepublic Product GET([FromODataUri]int key) { }