When sending complex javascript objects to the Web API, I noticed on a video that a typed object is used as a parameter, but it is not clear on the steps needed to take place to unpack the data and map fields correctly.
Should a C# class be create that matches the javascript fields exactly? Since javascript objects of a specific type are not always created equal will this throw an error on the server side?
Example:
Two javascript objects of the same type.
function Person(){
this.name='';
this.habbits='';
}
var person = new Person();
person.name='mark'
person.habbits='girls'
//
var personTwo=new Person();
personTwo.name='jane';
personTwo.habbits='men';
personTwo.car='red';
Notice a .car property was added dynamically
What is the best way to approach this type of situation? Just create a C# class with all the possiable properties that could be passed to the WEB API?
What is the best way to approach this type of situation? Just create a C# class with all the possiable properties that could be passed to the WEB API?
It will be better, but if you cannot handle dynamic properties then you use dictionary.
javascript
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Marked as answer by erikkl2000 on Jun 19, 2012 03:01 PM
erikkl2000
Member
257 Points
234 Posts
Web API & Javascript Complex Objects
May 20, 2012 04:24 PM|LINK
When sending complex javascript objects to the Web API, I noticed on a video that a typed object is used as a parameter, but it is not clear on the steps needed to take place to unpack the data and map fields correctly.
Should a C# class be create that matches the javascript fields exactly? Since javascript objects of a specific type are not always created equal will this throw an error on the server side?
Example:
Two javascript objects of the same type.
function Person(){ this.name=''; this.habbits=''; } var person = new Person(); person.name='mark' person.habbits='girls' // var personTwo=new Person(); personTwo.name='jane'; personTwo.habbits='men'; personTwo.car='red';Notice a .car property was added dynamically
What is the best way to approach this type of situation? Just create a C# class with all the possiable properties that could be passed to the WEB API?
Thanks!
javascript
aliostad
Member
228 Points
55 Posts
Re: Web API & Javascript Complex Objects
May 20, 2012 09:28 PM|LINK
There is a JObject part of Json.NET that is used by ASP.NET Web API (you need to get a recent nightly build to be able to use it).
JObject is a dynamic object so that you can access any ad hoc property from it.
See Henrik's blog for more info:
http://blogs.msdn.com/b/henrikn/archive/2012/05/03/asp-net-web-api-updates-may-3.aspx
javascript
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: Web API & Javascript Complex Objects
May 22, 2012 02:15 AM|LINK
It will be better, but if you cannot handle dynamic properties then you use dictionary.
javascript
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD