2
votes

I am using an ApiController (not an ODataController) with OData v4, in which there is a Get action taking ODataQueryOptions as a parameter, like the following:

public Product[] Get(ODataQueryOptions<Product> queryOptions){
    // Do query ...
}

When I send the oData query request to the ApiController as a uri via web api call, the ODataQueryOptions will be automatically populated. However, when the query string is very long, I get exceptions. I believe the exceptions are caused by that the length of the uri exceeds the limit. I am thinking about passing over the odata query string via the request body, instead of via the uri directly, hoping to bypass the uri length limit problem. E.g.,

public Product[] Get([FromBody] uriString){
    // var oDataQueryOptions = SomeMethod(uriString);
    // Do query ...
}

It seems one can construct ODataQueryOptions using ODataQueryContext and HttpRequestMessage:

public ODataQueryOptions(
    ODataQueryContext context,
    HttpRequestMessage request
)

ODataQueryContext in turn can be constructed using

public ODataQueryContext(
    IEdmModel model,
    Type elementClrType,
    ODataPath path
)

I am able to get the model (IEdmModel) and elementClrType (Type). However, I don't know how to get ODataPath from the query uri.

Also, can I construct a HttpRequestMessage using the uri string (which might be very long)?

Again, I am using an ordinary ApiController with OData V4.

Any help will be greatly appreciated!

1

1 Answers

1
votes
  1. About generating ODataPath, you can use ODataPath DefaultODataPathHandler.Parse(IEdmModel model, string serviceRoot, string odataPath). The odatapath is the url escaped service root and query options part.

  2. The constuctor of HttpRequestMessage is quite straightforward, for example: new HttpRequestMessage(HttpMethod.Get, "http://example.com").

But i'm not sure if you can bypass the query url length limitation with this way. I thought WebAPI OData actually do the same thing with what you are trying to do.