4

i need to create a custom query option and to be honest i have no idea where to start from. i don't know how they are created or what class handles them. On my project i have used markdown but i also need to enable ( in which case the text marked will be markdown ) or disable ( in this case i will have plain text ).

at this point my solution was to send a parameter to a function and tell it when to enable / disable

EX:

.../.../Namespace.MyFunction(markdown=1)

but i'm looking for a way to obtain this

.../.../...?$markdown=true

something like count query option.

Thank you

2 Answers 2

6

The dollar sign prefix should only be used with system query options. Think of $ as a reserved namespace. You can certainly use your own application-specific query options; just don't prefix them with $.

To get the value of markdown from a request URI like http://host/path?markdown=true from within a controller method, use the GetQueryNameValuePairs extension method. See also How to access all querystring parameters as a dictionary.

Sign up to request clarification or add additional context in comments.

Comments

3

Since you're using ASP.NET, you can also just use regular parameter binding.

For example, an ODataController action method with the following prototype:

[EnableQuery]
public IQueryable<MyEntity> Get(string testParam = "")

will service a request at the following URL:

http://your.machine/api/odata/myentity?$count=true&$top=10&$skip=0&testParam=true

The OData parameters $count, $top, and $skip will all be honored, and your custom testParam will have the string "true" assigned within the Get method.

1 Comment

Please note that it seems the order of the query parameters is important. If I place the custom query parameters at the beginning of the query, they end up being null and the ODataQueryOptions object also ends up invalid: public IQueryable<MyEntity> Get(ODataQueryOptions<MyEntity> queryOptions, string testParam = ""). In my case this worked: http://your.machine/api/odata/myentity?$count=true&$top=10&$skip=0&testParam=true; this didn't: http://your.machine/api/odata/myentity?testParam=true&$count=true&$top=10&$skip=0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.