【发布时间】:2015-05-24 19:24:32
【问题描述】:
我正在尝试创建一个支持强制转换操作的 ODataController。
假设我们有派生自 Shape 的 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。
如何让我的控制器支持投射?或者我的方法完全错误,我误解了原则?
谢谢
【问题讨论】:
标签: c# odata asp.net-web-api2