RSS

Tag Archives: odata

ASP.NET Web API and OData’s Verbose JSON

OData v3 and Verbose JSON

Whenever you work with OData in ASP.NET Web API, the return result will be JSON format. It’s noteworthy to know that by default Web API will return new OData JSON format specified in OData version 3. For older OData, the JSON format is different. It has more ‘unnecessary’ placeholders. This old OData JSON is also referred as ‘Verbose JSON’. In short, there are two types of OData JSON:

  • OData v3 has new JSON format
  • Verbose JSON is used in older OData versions.

ASP.NET Web API (.Net 4.5) and Verbose JSON

As mentioned before, by default Web API will return new OData JSON.

To change this behavior, you can request Web API to return old OData JSON format by specifying ‘odata=verbose’ in Accept header. Below is the sample of jQuery Ajax call specifying verbose JSON:

$.ajax({
    url: "/api/Products",
    dataType: "json",
    accepts: { json: "application/json;odata=verbose" }
}).done(function() {
    // do work ...
});

How it Looks Like?

For the curious:

Verbose JSON

{
    "d" : {
        "results": [
            {
                "__metadata": {
                    "uri": "http://demos/Orders(10248)",
                    "type": "SampleModel.Order"
                },
                "ProductId": 10248,
                "Name": "Vgnon"
            }
        ],
        "__count": "1"
    }
}

OData v3 (and above) JSON

{
    "odata.metadata":"http://localhost:43111/api/$metadata#Products",
    "odata.count":"1",
    "value":
    [
        {
            "ProductId":2,
            "CreatedDate":"2013-06-10T23:53:26",
            "CreatedBy":0,"Name":"Camera"
        }
    ]
}
Advertisements
 
Leave a comment

Posted by on October 7, 2013 in General

 

Tags: , ,

The Complex Type Refers to Entity Type Through the Property

This applies to ASP.NET Web API under MVC 4 and .Net 4.5

Similar to Type Deriving From error, this error message comes from ASP.NET Web API when I implemented using ODataConventionModelBuilder.

the-complex-type-refers-to-entity-type-through-the-property

GetEdmModel() is my ODdataConventionModelBuilder and what causing this is that I didn’t expose EntitySet that my complex type refers to, Account refers to User.

To get around this, expose EntitySet in ODataConventionModelBuilder.

ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();

// Exposed enitites to Odata
modelBuilder.EntitySet<Account>("Accounts");
modelBuilder.EntitySet<User>("Users");

There issue tracked on AspNetWebStack CodePlex

 
Leave a comment

Posted by on March 5, 2013 in General

 

Tags: , ,

Cannot Define Keys on Type Deriving From. Only Root Type in Entity Inheritance Hierarchy Can Contain Keys

This applies to ASP.NET Web API under MVC 4 and .Net 4.5

This error message is from ASP.NET Web API when I implemented using ODataConventionModelBuilder.

cannot-define-keys-on-type-deriving-from-only-root-type-in-entity-inheritance-hierarchy-can-contain-keys

GetEdmModel() is my ODdataConventionModelBuilder and what causing this is that I didn’t specify .DerivesFrom() on the type that inherit from others. In my case, Driver inherits from User.

To get around this, I have to include .DerivesFrom() in ODataConventionModelBuilder.

ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();

// Derived class must be specified.
modelBuilder.Entity<Driver>().DerivesFrom<User>();
modelBuilder.Entity<Customer>().DerivesFrom<User>();

// Exposed enitites to Odata
modelBuilder.EntitySet<User>("Users");

More on OData in Web API alpha release, “ODataConventionModelBuilder and inheritance”

 
1 Comment

Posted by on February 28, 2013 in General

 

Tags: , ,

OData System Query Options

OData system queries are:

Source: OData

 
Leave a comment

Posted by on April 11, 2012 in References

 

Tags: ,

 
%d bloggers like this: