I have a web API project that has been used for several years without OData support, only with standard URL parameters.
Now I want to add OData support for this API, but since the API is not built on the requested model, it is supposed to get the object ODataQueryOptions<T>and transfer it to the repository.
All I can find to read about OData support assumes that I have the requested model or too simplified and just tells me how to understand the meaning of the object ODataQueryOptions. Therefore, I cannot run a simple method.
Here is what I have now.
[Route("test")]
[HttpGet]
[EnableQuery]
public IHttpActionResult Test(ODataQueryOptions<TestOptions> options)
{
var settings = new ODataValidationSettings {
AllowedFunctions = AllowedFunctions.None,
AllowedLogicalOperators = AllowedLogicalOperators.Equal,
AllowedArithmeticOperators = AllowedArithmeticOperators.None,
AllowedQueryOptions = AllowedQueryOptions.Filter
};
try
{
options.Validate(settings);
}
catch (ODataException exception)
{
return BadRequest(exception.Message);
}
var binaryOperator = options.Filter?.FilterClause?.Expression as BinaryOperatorNode;
if (binaryOperator != null)
{
var property = binaryOperator.Left as SingleValuePropertyAccessNode ?? binaryOperator.Right as SingleValuePropertyAccessNode;
var constant = binaryOperator.Left as ConstantNode ?? binaryOperator.Right as ConstantNode;
if (property?.Property != null && constant?.Value != null)
{
;
}
}
return Ok();
}
The class TestOptions(in the parameter ODataQueryOptions<TestOptions>) is an empty class:
public class TestOptions
{
}
I also added
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.AddODataQueryFilter();
}
}
However, after calling this from the REST API client ...
{
"": " .",
"ExceptionMessage": " OData.",
"ExceptionType": "System.InvalidOperationException",
"StackTrace": "..."
}
? , , , OData global.asax , , -OData-, , (.. - OData).