If you are fine with only using the built-in JSON media type formatter (no XML, Json.NET etc) then you can take a parameter of type JsonValue and call AsDynamic on it to get a dynamic variable representing the content. If you don't want to lock yourself
into using only System.Json, you could take a parameter of type IKeyValueModel. It's not as easy to work with but it gives you essentially the same functionality. It probably wouldn't be too difficult to map an IKeyValueModel to a dynamic.
kloojed
Member
9 Points
6 Posts
ASP.NET Web API controller can't handle dynamic objects?
Feb 24, 2012 04:27 PM|LINK
In my code I have a base class Foo and all my objects inherits from the Foo object. So let's say I have a class like this
I have tried to use the ApiControllers put method with dynamic but I get this errorhttp://paste2.org/p/1914054
This is the code I'm using in the ApiController
If I use a normal controller I can use dynamic to post data. How can I make it work with dynamic objects?
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: ASP.NET Web API controller can't handle dynamic objects?
Feb 24, 2012 09:28 PM|LINK
Your input pararameters cannot be dynamic in Web API (nor can they in MVC, I'm not sure why you think they can).
kloojed
Member
9 Points
6 Posts
Re: ASP.NET Web API controller can't handle dynamic objects?
Feb 25, 2012 07:00 AM|LINK
I'm not thinking it works, I know it works in MVC 3. This works just perfect here so my question is still why it doesn't work with the API controller?
dcstraw
Member
37 Points
21 Posts
Re: ASP.NET Web API controller can't handle dynamic objects?
Mar 13, 2012 07:15 PM|LINK
If you are fine with only using the built-in JSON media type formatter (no XML, Json.NET etc) then you can take a parameter of type JsonValue and call AsDynamic on it to get a dynamic variable representing the content. If you don't want to lock yourself into using only System.Json, you could take a parameter of type IKeyValueModel. It's not as easy to work with but it gives you essentially the same functionality. It probably wouldn't be too difficult to map an IKeyValueModel to a dynamic.
marcind
Contributor
3344 Points
609 Posts
Microsoft
Re: ASP.NET Web API controller can't handle dynamic objects?
Mar 13, 2012 09:23 PM|LINK
Your sample code does not actually use the editorModel parameter. The value is probably just null.
Don't use IKeyValueModel. We are going to remove that interface.
ASP.NET Team
@marcind
Blog