I did something similar with WCF and asp.net mvc 3.5 data services, but that was a bit of a shred.
The first step is to rewrite the path so that skipping and vertex options are not applied twice, once by you and once at run time.
I rewrote the HttpModule. In the BeginRequest method, you will have the following code:
HttpApplication app = (HttpApplication)sender; if (HttpContext.Current.Request.Path.Contains(YOUR_SVC)) { if (app.Request.Url.Query.Length > 0) { //skip questionmark string queryString = app.Request.Url.Query.Substring(1) .Replace("$filter=", "filter=") .Replace("$orderby=", "orderby=") .Replace("$top=", "top=") .Replace("$skip=", "skip="); HttpContext.Current.RewritePath(app.Request.Path, "", queryString); } }
Then just browse the query line and pull out the necessary parameters.
if (HttpContext.Current.Request.QueryString["filter"] != null) var filter = HttpContext.Current.Request.QueryString["filter"] as string;
Then split the filter string and parse it in the sql statement or any other db commands. I used NHibernate in my case. I was also able to limit which filter commands I supported, which simplified the situation. For example, I did not do groupings. Mostly just comparison operators.
There is a list of filter operators in OData.org . Divide the string into "and" and "or" into separate sentences. Separate each sentence with a space, and you should get a 3-element array with the property name in [0] by the operator in [1] and the value in [2].
Articles will be in terms of Persons, but I assume that you can convert them to something that will output the correct results from db.
I ended up abandoning this approach, as it will not work for POSTS. I had to write my own Linq provider, but I wrote my own filter parser to make this easier to understand.
Jason Freitas May 28 '12 at 12:24 2012-05-28 12:24
source share