修改后的查询结果ODATA [英] Modify odata results after query

查看:141
本文介绍了修改后的查询结果ODATA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用实体框架和一个ASP.NET Web API应用程序的OData。

I have an ASP.NET Web API app using Entity Framework and Odata.

我想使用时,得到...目前,在控制器内修改查询的结果,你只是有效传递的EntityFramework数据实体回OData的处理器...

I would like to modify the results of a query when using a GET... currently, within the controller, you effectively just pass the EntityFramework data entities back to the Odata handlers...

[EnableQuery]
public IQueryable<myEntity> GetLineItem()
{
    return db.myEntities;
}

这是简单到任何查询的OData通过简单地返回一个子集,通过这个prePEND

It's simple to prepend whatever query Odata passes into this by simply returning a subset

return db.myEntity.Where(myEntity => myEntity.Name == "Bob")

的OData将新增无论是在$过滤器查询字符串参数来这里传递的前pression,你会得到这些结果的子集。

Odata will add whatever is in the $filter querystring parameter to the expression passed in here and you get the subset of these results.

不过,我想遍历结果一旦执行查询和SQL结果被解析成实体对象。

However, I would like to iterate over the results once the query executes and the SQL results are parsed into entity objects.

我曾尝试创建实现的IQueryable接口的包装,并钩住的GetEnumerator方法和同为IProvider和挂钩到execute方法。 ODATA似乎没有使用这些任何一个。

I have tried creating a wrapper that implements the IQueryable interface and hooking into the GetEnumerator methods and the same for the IProvider and hooking into the execute method. Odata doesn't seem to be using either one of these.

有没有办法做到这一点?

Is there a way to do this?

推荐答案

您可以通过实现一个过滤器做的。过滤器是简单的,你可以申请一个动作(控制器的方法)的属性附加伤害,控制器(控制器类)或注册为整个应用程序。

You can do that by implementing a filter. A filter is simply an atribute that you can apply to an action (controller's method), a controller (controller's class) or register for the whole application.

这个过滤器在管道上的一些步骤中应用:有一个管道,从传入的HTTP请求到控制器的动作去和回的反应,您可以在这条管道的异地过滤器,以修改流经这条管道的数据。

This filter is applied at some step on the pipeline: there's a pipeline that goes from the incoming HTTP request to the controllers action, and back to the response, and you can include a filter in different places of this pipeline, to modify the data flowing through this pipeline.

您特别需要继承 ActionFilterAttribute ,并做你的后期处理的 OnActionExecuted 这之后,控制器执行行动已执行。

You particularly need to inherit ActionFilterAttribute, and do your post processing on the OnActionExecuted which is executed after the controller's action has been executed.

文件:

  • ActionFilterAttribute
  • ActionFilterAttribute.OnActionExecuted
  • HttpActionExecutedContext Class

最后一类是 OnActuonExecuted 方法的参数的类型,并包含您可以修改响应。

The last class is the type of the parameter of the OnActuonExecuted method, and contains the response that you can modify.

这篇文章的第一部分包含了行为过滤器的解释:
<一href=\"https://damienbod.word$p$pss.com/2014/01/04/web-api-2-using-actionfilterattribute-overrideactionfiltersattribute-and-ioc-injection/\"相对=nofollow> WEB API 2使用ACTIONFILTERATTRIBUTE,OVERRIDEACTIONFILTERSATTRIBUTE和IOC注射

The first part of this article contains an explanation for action filter: WEB API 2 USING ACTIONFILTERATTRIBUTE, OVERRIDEACTIONFILTERSATTRIBUTE AND IOC INJECTION

这篇关于修改后的查询结果ODATA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆