MapODataRoute和ODataQueryOptions [英] MapODataRoute and ODataQueryOptions
问题描述
我正在构建一个处理无类型实体对象的WebAPI OData解决方案,如
Im building a WebAPI OData solution that handles untyped entity objects, as described in this excellent post. Like that post, I define my EdmModel upfront, and use the MapODataRoute method and pass in the model to use:
config.Routes.MapODataRoute("odata", "odata", ModelBuilder.GetEdmModel());
但是,这似乎不适用于我的方法中的ODataQueryOptions参数:
However, this does not seem to work with ODataQueryOptions parameter in my methods:
Get(ODataQueryOptions query)
{
}
它给出以下错误:给定的模型不包含类型'System.Web.Http.OData.IEdmEntityObject'.参数名称:elementClrType
有什么方法可以使ODataQueryOptions与MapODataRoute一起使用?
Is there any way to get ODataQueryOptions to work with MapODataRoute?
推荐答案
您应该在无类型模式的控制器操作中手动构建ODataQueryOptions.示例代码如下,
You should build the ODataQueryOptions manually in your controller action in untyped mode. Sample code follows,
ODataPath path = Request.GetODataPath();
IEdmType edmType = path.EdmType;
IEdmType elementType = edmType.TypeKind == EdmTypeKind.Collection
? (edmType as IEdmCollectionType).ElementType.Definition
: edmType;
// build the typeless query options using the element type.
ODataQueryContext queryContext = new ODataQueryContext(Request.GetEdmModel(), elementType);
ODataQueryOptions queryOptions = new ODataQueryOptions(queryContext, Request);
这篇关于MapODataRoute和ODataQueryOptions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!