ODataQueryOptions support in existing web interface

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)
    {
        // existing code

        config.AddODataQueryFilter();
    }
}

However, after calling this from the REST API client ...

{
     "": " .",
     "ExceptionMessage": " OData.",
     "ExceptionType": "System.InvalidOperationException",
     "StackTrace": "..."
}

? , , , OData global.asax , , -OData-, , (.. - OData).

+4
2

, EntityDate . List<Poco.Language>, .AsQueryable() .

[Route(""), HttpGet]
public IHttpActionResult  Get(ODataQueryOptions<Poco.Language> queryOptions)
{          
    return Ok(queryOptions.ApplyTo(_repository.GetAll().AsQueryable()));
}

OData, WebApiConfig.

Poco.Language - # POCO.

+3

?

public void Configuration(IAppBuilder appBuilder)
        {
            // Set up server configuration
            var config = new HttpConfiguration();
            config.Routes.MapODataRoute(routeName: "OData", routePrefix: "odata", model: GetEdmModel());
            appBuilder.UseWebApi(config);
        }

        private IEdmModel GetEdmModel()
        {
            var modelBuilder = new ODataConventionModelBuilder();
            modelBuilder.EntitySet<Customer>("customer");
            modelBuilder.EntitySet<Order>("order");
            modelBuilder.EntitySet<Customer>("response");
            return modelBuilder.GetEdmModel();
        }

, , .Net Core Web Api. https://github.com/OData/ODataSamples, , , . ODataQueryableSample.csproj. EntityFramework, .

, , [EnableQuery] ODataQueryOptions - .

, , , (, IApplicationBuilder IAppBuilder). , .

0

Source: https://habr.com/ru/post/1680639/


All Articles