How to Instantiate ODataQueryOptions

偶尔善良 提交于 2019-11-30 21:17:08

Sure. ODataPath is a list of ODataPathSegment(s) which should follow up the OData Uri spec.

In Web API OData, it's easy to instantiate an ODataPath, for example:

IEdmModel model = GetEdmModel(); 
IEdmEntitySet entitySet = model.EntityContainer.FindEntitySet(setName); 
ODataPath path = new ODataPath(new EntitySetPathSegment(entitySet)); 

The above path follows up the OData spec that it has the odata template as:

~/entityset

More test cases (codes) can be found here

Your odata controller is providing an HTTP interface to your data, shouldn't you access it via HTTP (even if from the server)? There is a VS add-in to generate odata client code here:

https://visualstudiogallery.msdn.microsoft.com/9b786c0e-79d1-4a50-89a5-125e57475937

Or, if you are doing this from within the same project, why not a common method that returns the IQueryable that could be called from your code or from the controller?

UPDATE: Based on more information in the original question:

If you have ODataQueryOptions defined in a controller method, it will allow you to parse the well-formed odata query that is calling that method. I have used this when I needed to translate parts of an odata query because I needed to query multiple data sources to return the final result.

It sounds like you want something that takes non-odata parameters and options. For this, you probably need to look at custom Actions and or Functions (if you are just returning data, probably a function):

http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/odata-actions-and-functions

UPDATE #2: And after more detailed reading, I think I missed your point - I don't have the answer, but I'll play around with it. Can't you just reform the URL itself (as opposed to instantiating the query options?

UPDATE #3: I think you are going to have a hard time tricking it into thinking it is getting an odata request...that isn't really an odata request. Back to the second option mentioned in my original answer - why not a common method that you can use and the odata controller can use - something like this:

// some sort of helper class
public class HelperObject
{
    public static IQueryable<MyType> GetGroupedValues(int filterID, string groupByFieldName)
    {
        // all your code/logic here
    }
}

// your odata controller uses the helper
[HttpGet]
[Route("myTypes/{filterID:int}/groupby/{groupByFieldName}")]
public IHttpActionResult GroupMyTypes(int filterID, string groupByFieldName)
{
    return Ok(HelperObject.GetGroupedValues(filterID, groupByFieldName));
}

// ... and so does your other code that wants to do the same thing
var x = HelperObject.GetGroupedValues(filterID, groupByFieldName);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!