5
votes

I would like to "intercept"/alter the OData query that's generated when using OData with the Web API.. but I'm not entirely sure of how to "extract" the generated query.. I assume that the OData filter,expands and more some how gets generated to some sort of expression tree or some sort of query.. and if that's the case, then that's the type of query I would like to be able to alter before its sent to the database as an SQL-command.

I have searched the net for some way of extracting the generated expression tree.. but hasn't be able to find sufficient information, so I was sort of hoping that someone here has some more insight of how the whole OData-"framework" works..

Any ideas of where to start?

1
show us what you have triedBRAHIM Kamel
@Inx51 Any luck on this?johnny 5

1 Answers

2
votes

You can alter Odata url before it is executed. Inherit from EnableQueryAttribute class and change url. Following is a real case to change guid string format to hexadecimal string format before it's sent to oracle.

public class EnableQueryForGuid : EnableQueryAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        var url = actionContext.Request.RequestUri.OriginalString;

        var newUrl = ModifyUrl(url);

        actionContext.Request.RequestUri = new Uri(newUrl);
        base.OnActionExecuting(actionContext);
    }

    private string ModifyUrl(string url)
    {
        Regex regex = new Regex(@"%27([A-Za-z0-9]{32})%27");
        var res = regex.Matches(url);
        if (res.Count > 0)
        {
            var guidPart = res[0].Value.Remove(0, 3);
            guidPart = guidPart.Remove(guidPart.Length - 3, 3);
            var guidValue = new Guid(BitConverter.ToString((new Guid(guidPart)).ToByteArray()).Replace("-", ""));
            url = url.Replace(res[0].Value, guidValue.ToString());
        }
        return url;
    }
}

then use this new attribute on your controller method:

    [HttpGet]
    [EnableQueryForGuid]
    [ODataRoute("GetSomething")]
    public IHttpActionResult GetSomething()
    {
      ....
    }

original query:

OData/GetSomething?&$filter=MyGuid%20eq%20%272C3C7BC0EC7FA248B0DEE3DAA371EE73%27

altered query:

OData/GetSomething?&$filter=MyGuid%20eq%20e5794d6a-5db1-475a-8c49-0f91a8f53c8a