Changing the results are after request

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

I would like to change the query results when using GET ... currently inside the controller, you just simply pass EntityFramework data entities back to Odata handlers ...

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

It’s easier to add any query that Odata goes into this by simply returning a subset

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

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

However, I would like to repeat the results after executing the query and the SQL results are processed in entity objects.

I tried to create a wrapper that implements the IQueryable interface and connects to GetEnumerator methods, and the same goes for IProvider and connects to the execute method. It seems that Odata does not use any of them.

Is there any way to do this?

+5
source share
3 answers

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

This filter is applied at some stage in the pipeline: there is a pipeline that goes from the incoming HTTP request to the action of the controllers and back to the response, and you can enable the filter in different places of this pipeline to change the data passing through this pipeline.

You especially need to inherit the ActionFilterAttribute and do your post-processing on the OnActionExecuted , which is executed after the controller action is completed.

Docs:

The last class is a parameter type of the OnActuonExecuted method and contains an answer that you can change.

The first part of this article describes the action filter: WEB API 2 USING ACTIONFILTERATTRIBUTE, OVERRIDEACTIONFILTERSATTRIBUTE AND INIECTION IOC

+6
source

You do not need to return IQuerable from the controller when working with OData. Check the box “Call query parameters directly” in https://docs.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options For your case, it will look like this:

 public HttpResponseMessage Get(ODataQueryOptions<myEntity> opts) { var settings = new ODataValidationSettings(); opts.Validate(settings); var intermediateResult = opts.ApplyTo(db.myEntities).ToArray(); var result = //change intermediateResult as you wish. return result; } 
0
source

Based on Ihar's answer , here is a complete example of applying query parameters and then modifying the resulting collection.

 public async Task<IHttpActionResult> Get(ODataQueryOptions<MyModel> options) { var query = _service.Query(); var result = ((IQueryable<MyModel>)options.ApplyTo(query, new ODataQuerySettings())) .ToArray(); foreach (var model in result) { model.SomeNonQueriedValue = "Something else"; } return Ok(result); } 
0
source

All Articles