Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does WebAPI 2.2 OData v4 [EnableQuery] apply?

Where is it correct/incorrect to apply the EnableQueryAttribute as of Jan 2015?

The document linked below:

http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-endpoint

Says:

The [EnableQuery] attribute enables clients to modify the query, by using query options such as $filter, $sort, and $page. For more information, see Supporting OData Query Options.

The following linked document:

http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options

Says:

The EnableQuerySupport method enables query options globally for any controller action that returns an IQueryable type.

But this document for OData 4 on WebApi 2.2 has put it on actions returning IHttpActionResult:

http://blogs.msdn.com/b/webdev/archive/2014/03/13/getting-started-with-asp-net-web-api-2-2-for-odata-v4-0.aspx

[ODataRoutePrefix("Teams")]
public class TeamsEntitySetController : ODataController
{
    private readonly LeageContext _leage = new LeageContext();

    [EnableQuery]
    [ODataRoute]
    public IHttpActionResult GetFeed()
    {
        return Ok(_leage.Teams);
    }
    [ODataRoute("({id})")]
    [EnableQuery]
    public IHttpActionResult GetEntity(int id)
    {
        return Ok(SingleResult.Create<Team>(_leage.Teams.Where(t => t.Id == id)));
    }
}

I'm going crazy trying to find up-to-date, accurate and consistent documentation on OData v4 / WebApi 2.2.

Which is correct today?

like image 996
Luke Puplett Avatar asked Jan 09 '15 12:01

Luke Puplett


1 Answers

Use global configuration (instance of an HttpConfiguration object) and call

config.Filters.Add(new EnableQueryAttribute()
            {
                PageSize = 2
                // .. other settings
            });

this works

like image 165
xxxmatko Avatar answered Oct 12 '22 01:10

xxxmatko