OData AspNetCore support for long URLs useing $query is not working

Viewed 462

As it is been pointed in this article, with $query keyword you can solve the problem of long OData URLs after the version 7.5 of Microsoft.AspNetCore.OData.

but in my project(I'm using version 8.preview3) I tried the use this but I keep getting 404 not found error. Here are my working URL examples:

https://localhost:44346/odata/WFC/APost/
https://localhost:44346/odata/WFC/APost/?$select=id
...

but when I use a POST request with $query I receive 404 error:

    POST: https://localhost:44346/odata/WFC/APost/$query
    Content-Type: text/plain
    Request Body: $select=id

I wonder if I have to add any code to enable the $query functionality !?

Here are relevant pieces of my code :

     // version 8.preview3 of Microsoft.AspNetCore.OData code

     services.AddOData(
           opt => opt.AddModel("odata", GetEdmModel()).Select().Count().Filter().OrderBy().SetAttributeRouting(true));
    
    ...
    
     public IEdmModel GetEdmModel()
            {
                var builder = new ODataConventionModelBuilder();
                var s = builder.EntitySet<WeatherForecast>("WFC").EntityType;
                s.Collection.Action("APost");
                return builder.GetEdmModel();
            }

and the controller :

    [ODataRoutePrefix("WFC")]
    public class WeatherForecast2Controller : ODataController
    {
     ...
        [HttpPost]
        [EnableQuery]
        [ODataRoute("APost")]
        public IEnumerable<WeatherForecast> APost()
        {
            // this function just returns array of 10 WeatherForecast(s) . 
            ...
        }

    }
1 Answers

You don't have to create a POST endpoint. The POST .../$query request should be translated to the GET endpoint.

However, you need to use the middleware:

// Startup.cs
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
  app.UseODataQueryRequest();
}

For more info refer https://github.com/OData/WebApi/issues/2414#issuecomment-1021571407.

Related