Web API 2.2-OData v4(手动解析Uri和扩展)

Web API 2.2-OData v4(手动解析Uri和扩展),odata,asp.net-web-api,expand,Odata,Asp.net Web Api,Expand,我有一个ODataController,其Get方法如下: public IHttpActionResult Get(ODataQueryOptions<MyModel> queryOptions) { IQueryable<MyModel> models = _Models.AsQueryable(); // _Models Defined in Controller as List<MyModel> and is already populated w

我有一个ODataController,其Get方法如下:

public IHttpActionResult Get(ODataQueryOptions<MyModel> queryOptions) {
  IQueryable<MyModel> models = _Models.AsQueryable(); // _Models Defined in Controller as List<MyModel> and is already populated with nested data for both .LevelOne and .LevelOne.LevelTwo which are two other Lists.

  Uri fullrequest = Request.GetRequestContext().Url.Request.RequestUri; // http://localhost:8080/odata/Root?$expand=LevelOne($expand=LevelTwo)
  Uri serviceroot = new Uri(controller.GetLeftPart(UriPartial.Path).Replace("/Root", "")); // http://localhost:8080/odata
  String metadata = service + "/$metadata"; // http://localhost:8080/odata/$metadata

  IEdmModel model = EdmxReader.Parse(XmlTextReader.Create(metadata));
  ODataUriParser parser = new ODataUriParser(model, serviceroot, fullrequest);
  SelectExpandClause selectAndExpand = parser.ParseSelectAndExpand();

//Only one of the two below lines is ever commented in...
  Request.ODataProperties().SelectExpandClause = queryOptions.SelectExpand.SelectExpandClause; // This line will work
  Request.ODataProperties().SelectExpandClause = selectAndExpand; // This line will not work

  return Ok(models);
}
public IHttpActionResult获取(ODataQueryOptions查询选项){
IQueryable models=_models.AsQueryable();/_models在控制器中定义为列表,并且已经为.LevelOne和.LevelOne.LevelTwo这两个其他列表填充了嵌套数据。
Uri fullrequest=Request.GetRequestContext().Url.Request.RequestUri;//http://localhost:8080/odata/Root?$expand=LevelOne($expand=LevelTwo)
Uri serviceroot=新Uri(controller.GetLeftPart(UriPartial.Path).Replace(“/Root”,”);//http://localhost:8080/odata
字符串元数据=服务+“/$metadata”;//http://localhost:8080/odata/$metadata
IEdmModel model=EdmxReader.Parse(XmlTextReader.Create(metadata));
ODataUriParser解析器=新的ODataUriParser(模型、serviceroot、fullrequest);
SelectExpandClause selectAndExpand=parser.ParseSelectAndExpand();
//以下两行中只有一行在。。。
Request.ODataProperties().SelectExpandClause=queryOptions.SelectExpand.SelectExpandClause;//这行可以用
Request.ODataProperties().SelectExpandClause=selectAndExpand;//此行无效
返回Ok(型号);
}

使用手动解析的selectAndExpand不会扩展数据集,但使用预定义的查询选项可以扩展数据集。你知道为什么吗?在调试器中查看时,这两个对象似乎包含相同的信息,但我肯定遗漏了什么。我希望能够自己解析URI,而不需要ODataQueryOptions

我最后做的是基于原始请求构建一个新的ODataQueryOptions对象,然后从中提取SelectExpandClause子句。它并没有回答我最初的问题,但对于不必传入ODataQueryOptions参数来说,这是一个比较有效的解决方案。请参见下面的我的代码:

public IHttpActionResult Get() {
//Get Queryable Item (in this case just a list made queryable)
  IQueryable<MyModel> models = _Models.AsQueryable();

//Create new ODataQueryContext based off initial request (required to create ODataQueryOptions)
  ODataQueryContext selectAndExpandContext = new ODataQueryContext(Request.ODataProperties().Model, typeof(MyModel), Request.ODataProperties().Path);

//Create new ODataQueryOptions based off new context and original request
  ODataQueryOptions<Employee> selectAndExpandOptions = new ODataQueryOptions<Employee>(selectAndExpandContext, Request);

//Attach Select + Expand options to be processed
  if (selectAndExpandOptions.SelectExpand != null) {
    Request.ODataProperties().SelectExpandClause = selectAndExpandOptions.SelectExpand.SelectExpandClause;
  }

  return Ok(models);
}
public IHttpActionResult Get(){
//获取可查询项(在本例中,只是一个可查询的列表)
IQueryable模型=_models.AsQueryable();
//根据初始请求创建新的ODataQueryContext(创建ODataQueryOptions需要)
ODataQueryContext selectAndExpandContext=新的ODataQueryContext(Request.ODataProperties().Model,typeof(MyModel),Request.ODataProperties().Path);
//基于新上下文和原始请求创建新的ODataQueryOptions
ODataQueryOptions selectAndExpandOptions=新ODataQueryOptions(selectAndExpandContext,请求);
//附加选择+展开要处理的选项
if(selectAndExpandOptions.SelectExpand!=null){
Request.ODataProperties().SelectExpandClause=selectAndExpandOptions.SelectExpand.SelectExpandClause;
}
返回Ok(型号);
}

为了这个答案,我在互联网上搜索了两天!谢谢谢谢谢谢!!我在为Mongodb和odata v4实现SelectExpand时遇到了问题。Mongo的c#Iqueryable实现在ODataWebAPI的Select子句中遇到问题。这让它为我工作。我能够将过滤器应用于Mongo IQueryable,然后执行.ToList().AsQueryable(),然后像您一样应用SelectExpandClause。工作得很有魅力