18
votes

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?

1

1 Answers

9
votes

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

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

this works