我試圖創建一個支持投射操作的ODataController。Web API 2.2 OData控制器支持類層次結構
比方說,我們有Shape類和Circle類,從形狀
namespace NS
{
public abstract class Shape {
int Id;
int X;
int Y;
}
public class Circle : Shape {
int Radius;
}
}
派生,我想創建控制器ShapesController。
public class ShapesController: ODataController
{
ShapesContext db = new ShapesContext();
[EnableQuery]
public IQueryable<Shape> Get()
{
return db.Shapes;
}
[EnableQuery]
public SingleResult<Shape> Get([FromODataUri] int key)
{
IQueryable<Shapes> result = db.Shapes.Where(p => p.Id == key);
return SingleResult.Create(result);
}
}
一切工作的優良要求如
/odata/Shapes
/odata/Shapes(1)
但是要求像
/odata/Shapes(1)/NS.Circle
原因404錯誤
參考路由公約http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-routing-conventions我要創建這樣的
行動[EnableQuery]
public SingleResult<Circle> GetCircle([FromODataUri] int key)
{
IQueryable<Shapes> result = db.Shapes.Where(p => p.Id == key).Cast<Circle >;
return SingleResult.Create(result);
}
但它並不能幫助 - 404
我怎樣才能讓我的控制器支持鑄造?或者,也許我的方法是完全錯誤的,我誤解了這些原則?
感謝
這是一個錯誤。尾部的斜槓不應該被要求。 – lencharest