1
votes

I have a web api 2 project and want to expose the models as an Odata service so that it is queryable.

This is the setup I have done: In WebApiConfig.Register() I have

config.AddODataQueryFilter();

The following code is in the Values Controller

// GET api/values
[EnableQuery (PageSize=1)]
public IQueryable<TestProduct> Get()
{
    var str = new List<TestProduct>
    {
        new TestProduct {Name = "Dark", Value = "Lord"},
        new TestProduct {Name = "Dark", Value = "Elf"}
    };
    var hello = str.AsQueryable();
    return hello;
}

When I query

http://localhost:51823/api/values?$filter=Name eq 'Elf'

The result I get is:

[
    {
        "Name": "Dark",
        "Value": "Elf"
    }
]

I am missing the $odata.metadata properties and the inlinecount property. How can I add that to the response. Ideally i would be looking for:

    [
 "odata.metadata":"http://localhost:43111/api/$metadata#TestProducts",
            "odata.count":"1",
"value":
        {
            "Name": "Dark",
            "Value": "Elf"
        }
    ]

I don't plan on using EF.

1

1 Answers

1
votes

Try this:

http://localhost:51823/api/values?$filter=Name eq 'Elf'&$inlinecount=allpages

To get metadata info add the odata=fullmetadata to the Accept header eg:

Accept:application/json;odata=fullmetadata