【发布时间】:2015-09-24 05:40:26
【问题描述】:
我一直在提到 Asp.net WebApi 2.2 对 OData 的支持。 WebApi 处理很多 OData V4 协议相关的事情是非常有趣的。
我需要在没有 CLR 对象的情况下实现 Odata 服务。我在运行时了解我的类的属性。就像我有存储 Sql 查询的 xml 文件一样。我阅读了那些 xml 文件并在其中执行查询。阅读 XML 文件后,我意识到这些列并希望在 OData 服务下公开此信息。
我一直面临的挑战是我无法将 ODataQueryOptions 应用于 无类型(非 CLR) 对象。
示例代码https://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/OData/v4/ODataUntypedSample/ReadMe.txt 展示了如何从 WebApi OData 服务公开无类型对象,但它没有展示如何应用 ODataQueryOptions。
public class ProductsController : ODataController
{
private static IQueryable<IEdmEntityObject> Products = Enumerable.Range(0, 20).Select(i =>
{
IEdmEntityType productType = (IEdmEntityType)ODataUntypedModel.Model.FindType("AnalyticsPortal.Product");
EdmEntityObject product = new EdmEntityObject(productType);
product.TrySetPropertyValue("Id", i);
product.TrySetPropertyValue("Name", "Product " + i);
product.TrySetPropertyValue("Price", i + 0.01);
product.TrySetPropertyValue("Category", "Category - " + i);
return product;
}).AsQueryable();
///*
public EdmEntityObjectCollection Get()
{
//return productsContext.Products.AsQueryable();
var path = Request.ODataProperties().Path;
var edmType = path.EdmType;
var collectionType = edmType as IEdmCollectionType;
var entityType = collectionType.ElementType.Definition as IEdmEntityType;
var model = Request.ODataProperties().Model;
var queryContext = new ODataQueryContext(model, entityType, path);
var queryOptions = new ODataQueryOptions(queryContext, Request);
//Apply the query option on the IQueryable here.
//queryOptions
//How ??
//queryOptions.ApplyTo() work only on CLR types
//IQueryable<IEdmEntityObject>
return new EdmEntityObjectCollection(new EdmCollectionTypeReference(collectionType), Products.ToList());
}
//*/
}
如果有人能指出一些框架或插件可以处理 OData 查询选项(过滤器、选择、排序等)的方式,我将不胜感激
【问题讨论】:
-
你有没有找到解决这个问题的方法?我目前面临同样的问题。
-
@Arun 不幸的是没有。我使用 WCF(数据服务)项目来实现上述要求。
标签: c# .net asp.net-web-api odata