Web API & Javascript Complex Objectshttp://forums.asp.net/t/1805549.aspx/1?Web+API+Javascript+Complex+ObjectsTue, 22 May 2012 02:15:33 -040018055494988793http://forums.asp.net/p/1805549/4988793.aspx/1?Web+API+Javascript+Complex+ObjectsWeb API & Javascript Complex Objects <p>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.</p> <p>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?</p> <p>Example:</p> <p>Two javascript objects of the same type.</p> <pre class="prettyprint">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';</pre> <p><strong>Notice a .car property was added dynamically</strong></p> <p></p> <p>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?</p> <p></p> <p>Thanks!</p> <p></p> 2012-05-20T16:24:33-04:004988951http://forums.asp.net/p/1805549/4988951.aspx/1?Re+Web+API+Javascript+Complex+ObjectsRe: Web API & Javascript Complex Objects <p>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).</p> <p>JObject is a dynamic object so that you can access any ad hoc property from it.</p> <p>See Henrik's blog for more info:</p> <p><a href="http://blogs.msdn.com/b/henrikn/archive/2012/05/03/asp-net-web-api-updates-may-3.aspx">http://blogs.msdn.com/b/henrikn/archive/2012/05/03/asp-net-web-api-updates-may-3.aspx</a></p> 2012-05-20T21:28:46-04:004991079http://forums.asp.net/p/1805549/4991079.aspx/1?Re+Web+API+Javascript+Complex+ObjectsRe: Web API & Javascript Complex Objects <p></p> <blockquote><span class="icon-blockquote"></span> <h4>erikkl2000</h4> 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?</blockquote> <p></p> <p>It will be better, but if you cannot handle dynamic properties then you use dictionary.</p> 2012-05-22T02:15:33-04:00