Brent Pabst
Brent Pabst

Reputation: 1176

MVC 4 (RC) Web Api OData not sorting properly

Recently updated to the RC of MVC4. It fixed a lot of problems, especially around date serialization. However, I'm having another problem. The OData sorting and options I was using for paging/sorting, etc. are not working now. I haven't changed anything in the code and I can't find anything in the release notes that mentions the functionality changed. My code is below, any idea what I'm doing wrong?

Note: I am using the AttributeRouting extension, not sure if this is causing the problem or not.


Web API Class/Method

[RouteArea("v1")]
[RoutePrefix("locations")]
public class LocationsController : ApiController
{
    // Data Repository
    static readonly IRepository<Location> Repo = new LocationRepository();

    #region CRUD

    [GET("")]
    public IQueryable<Location> Get()
    {
        return Repo.All;
    }
}

jQuery Client Call

$.ajax({
        url: '/v1/locations?$orderby=Name asc',
        success: function (data) {
            resultFunction(data);
        }
});

JSON Response

[
    {
        "LocationId":"aca3e1fe-8192-4bb8-b233-1d6fe6b69ba4",
        "Name":"Triangle North"
    },
    {
        "LocationId":"0c99a267-2093-401a-9466-70788500630b",
        "Name":"Triangle West"
    },
    {
        "LocationId":"168e3755-b61e-41d6-99f3-941d738ab321",
        "Name":"Triangle East"
    },
    {
        "LocationId":"82e7a547-a4eb-4233-bdb4-cd8c5b369af8",
        "Name":"Triangle South",
    }
]

Upvotes: 2

Views: 1217

Answers (1)

Brent Pabst
Brent Pabst

Reputation: 1176

Turns out in the course for searching on this MSFT made the decision to require an explicit attribute to be specified in order for the OData stuff to flow automatically:

(...)You have to put a [Queryable] attribute on the method (docs).

[Queryable]
public IQueryable<Product> GetAllProducts()
{
    return repository.GetAll().AsQueryable();
}

http://forums.asp.net/t/1809900.aspx/1?Web+API+OData+in+RC

Doh!

Upvotes: 5

Related Questions