locked
OData 4 RRS feed

  • Question

  • User643096697 posted

    I'll start off by saying I'm new to OData. I do have a basic controller set up and working in my environment. I need to do a bit of custom work now. Before my OData service returns it's result set, I need to run a check on that data before I send it back to the client. So some custom code in the controller I assume. Every time this is called, I need to run this. In other words, there are no cases when this piece of data checking will not be done before returning data to the client. Is there a method I can override in ODataController or can I just throw the code into the controller I've created? (The Get?). I want to run this on the subset of data that has been filtered out by OData already.

    Thank you in advance!

    Michael 

    Tuesday, October 8, 2019 9:20 PM

All replies

  • User61956409 posted

    Hi Michael,

    Mike Bell

    Before my OData service returns it's result set, I need to run a check on that data before I send it back to the client.

    To achieve the above requirement, as you mentioned, you can implement custom code (or call custom function) in controller action. Besides, you can achieve same with ActionFilter, like below.

    public class RCheckerFilter : ActionFilterAttribute
    {
        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
            
            var objectContent = actionExecutedContext.Response.Content as ObjectContent;
    
            if (objectContent != null)
            {
                // the returned value
                var value = objectContent.Value; 
    
                //code logic here
            }
        }
    }

    With Regards,

    Fei Han

    Wednesday, October 9, 2019 7:02 AM
  • User643096697 posted

    This is exactly the kind of information I needed. I will try this later today. Thank you!

    Wednesday, October 9, 2019 3:02 PM
  • User643096697 posted

    My Attribute code never seems to get called:

    [TradeDataCheck]
            [EnableQuery]
            public IQueryable<TradePriceData> Get()
            {
                IQueryable<TradePriceData> results = db.TradePriceDatas.AsQueryable();
                //return db.TradePriceDatas.AsQueryable();
                return results;
            }
    public class TradeDataCheck : ActionFilterAttribute
        {
            public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
            {
    
                var objectContent = actionExecutedContext.Response.Content as ObjectContent;
    
                if (objectContent != null)
                {
                    // the returned value
                    var value = objectContent.Value;
    
                    //code logic here
                }
            }
    
            public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
            {
                base.OnActionExecuting(actionContext);
            }
        }

    Any suggestions? I need it to run before data is ever queried by the request itself (so I can run a data check before to ensure data is in a specific format already)

    Thursday, October 10, 2019 6:13 PM
  • User61956409 posted

    Hi Michael,

    Do you mean that you set breakpoint inside TradeDataCheck class and debug the code, which do not reach into code logic while you make request to action? Does it directly return action result to client while you make a GET request, or cause any error?

    The code snippet work well on my side, please try to create a new Web API application and test if it work well.

    With Regards,

    Fei Han

    Friday, October 11, 2019 6:41 AM
  • User643096697 posted

    I ended up just calling a normal web service. Time crunch, had to move on. I had set breakpoints that never got hit.

    Friday, October 11, 2019 6:08 PM
  • User61956409 posted

    Hi Michael,

    You can try to add the action filter within WebApiConfig class's Register method and check if it work for you.

    config.Filters.Add(new RCheckerFilter());

    With Regards,

    Fei Han

    Monday, October 14, 2019 5:03 AM